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}
};
输入班级人数,将每个人的
语文,数学,英语成绩输入二维数组
打印字:王,企:
string[,] shuzu = new string[,] {
{" ","■","■","■","■","■",""},
{" "," "," ","■","","",""},
{" "," "," ","■","","",""},
{" "," ","■","■","■","",""},
{" "," "," ","■"," "," ",""},
{" "," "," ","■"," "," ",""},
{"■","■","■","■","■","■","■"}
};
string[,] s = new string[,]{
{" "," "," ","●"," "," "," "},
{" "," ","●"," ","●"," "," "},
{" ","●"," ","●"," ","●"," "},
{"●"," "," ","●"," "," ","●"},
{" ","●"," ","●","●","●"," "},
{" ","●"," ","●"," " ," "," "},
{"●","●","●","●","●","●","●"}
};
转载于:https://www.cnblogs.com/xinghun/p/5269301.html
总结
以上是生活随笔为你收集整理的3.12课·········数组的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 蒙特卡洛法—非均匀随机数的产生
- 下一篇: Fetch API