欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

3.12课·········数组

发布时间:2025/3/14 编程问答 38 豆豆
生活随笔 收集整理的这篇文章主要介绍了 3.12课·········数组 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

数组:具有相同类型的若干变量按有序的形式组织起来的一种形式。这些按序排列的同类数据元素的集合称为数组。
定义数组
int[] 变量名 = new int [n];

string[] myStringArray = new string[6];

int[] myArray = new int[] {1, 3, 5, 7, 9};

数组索引号从0开始
一维数组[相同数据类型]
第一种定义方式
int[] shuzu=new int[5];
for (int i = 0; i <= 5;i++ )//用for循环为shuzu赋值
{
shuzu[i] = i + 1;
}
索引从0开始
赋值
shuzu[0] = 1;
shuzu[1] = 2;
shuzu[2] = 3;
shuzu[3] = 4;
shuzu[4] = 5;

第二种定义方式
int[] shuzu=new int[]{1,2,3,4,5};

Console.WriteLine(shuzu[2]);//3

输入10个人的年龄,并求和

int[] age = new int[10];int sum = 0;for (int i = 0; i < 10; i++){Console.Write("请输入第" + (i + 1) + "个人的年龄:");age[i]=int.Parse(Console.ReadLine());//获取这个数并转换成数值类型放进数组agesum += age[i];}Console.WriteLine("这10个人的年龄之和是" + sum);Console.ReadLine();

 输入人名放进数组,输出第五个人的名字

string[] s = new string[10];for (int i = 0; i < 10;i++ ){Console.Write("请输入第"+(i+1)+"个人的名字:");s[i] = Console.ReadLine();}Console.Write("第五个人的名字是"+s[4]);Console.ReadLine();

 输入班级人数,根据人数挨个输入成绩,求平均分

Console.Write("请输入班级人数:");int a = int.Parse(Console.ReadLine());doudouble[] chengji = new double[a];double sum = 0;for (int i = 0; i < a; i++){Console.Write("请输入第" + (i + 1) + "个人的成绩:");chengji[i] = double.Parse(Console.ReadLine());sum += chengji[i];}Console.WriteLine("" + a + "个人的平均分是" + sum / a); 

二维数组
int[,] shuzu = new int[3, 4];
int[,] shuzu = new int[,] {
{1,2,3,4},
{5,6,7,8},
{6,7,8,9}
};

输入班级人数,将每个人的
语文,数学,英语成绩输入二维数组

Console.Write("请输入班级人数:");int a = int.Parse(Console.ReadLine());double [,] shuzu = new double [a,3];for (int i = 0; i < a; i++){for (int j = 0; j < 3; j++){if (j == 0){Console.Write("请输入第"+(i+1)+"个人的语文成绩:");}if (j == 1){Console.Write("请输入第" + (i + 1) + "个人的数学成绩:");}if (j == 2){Console.Write("请输入第" + (i + 1) + "个人的英语成绩:");}shuzu[i, j] = double.Parse(Console.ReadLine());}}Console.ReadLine();

打印字:王,企:

string[,] shuzu = new string[,] {
{" ","■","■","■","■","■",""},
{" "," "," ","■","","",""},
{" "," "," ","■","","",""},
{" "," ","■","■","■","",""},
{" "," "," ","■"," "," ",""},
{" "," "," ","■"," "," ",""},
{"■","■","■","■","■","■","■"}
};

for (int i = 0; i < 7; i++){for (int j = 0; j < 7; j++){Console.Write(shuzu[i, j]);}Console.WriteLine();} 

string[,] s = new string[,]{
{" "," "," ","●"," "," "," "},
{" "," ","●"," ","●"," "," "},
{" ","●"," ","●"," ","●"," "},
{"●"," "," ","●"," "," ","●"},
{" ","●"," ","●","●","●"," "},
{" ","●"," ","●"," " ," "," "},
{"●","●","●","●","●","●","●"}
};

for (int i = 0; i < 7; i++){for (int j = 0; j < 7; j++){Console.Write(s[i, j]);}Console.WriteLine();}Console.ReadLine();

 

转载于:https://www.cnblogs.com/xinghun/p/5269301.html

总结

以上是生活随笔为你收集整理的3.12课·········数组的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。