//一维数组【相同数据类型】 //第一种定义方式 int []shuzu=new int[5] //索引从0开始 //赋值 shuzu[0] = 1; shuzu[1] = 2; shuzu[2] = 3; shuzu[3] = 4; shuzu[4] = 5; //shuzu[5]=6;----错误的,索引从0开始,到索引值4就结束了。 //第二种定义方式 int[] shuzu1 = new int[] { 1, 2, 3, 4, 5 }; Console.WriteLine(shuzu1[2]);
输入人名放进数组,输出第5个人的姓名
Console.WriteLine("请输入班级人数:"); int a = int.Parse(Console.ReadLine()); string[] name = new string[a]; for (int i=0;i<a ;i++ )//用for循环赋值给name数组 { Console.WriteLine("请输入第"+(i+1)+"个人的姓名:"); name[i]=Console.ReadLine(); } Console.WriteLine(name[4]);
//二维数组
int[,] shuzu = new int[3, 4];----有3个二维数组,每个二维数组又有4个一维数组;
int[,] shuzu = new int[,] {
{1,2,3,4},
{5,6,7,8},
{6,7,8,9}
};
输入班级人数,将每个人的语文,数学,英语成绩输入二维数组
Console.Write("输入班级人数:"); int renshu = int.Parse(Console.ReadLine()); double[,] shuzu = new double[renshu, 3]; for (int i = 0; i < renshu; i++) { for (int j = 0; j < 3; j++) { if (j == 0) { Console.WriteLine("请输入第" + (i + 1) + "个人的语文成绩:"); } if (j == 1) { Console.WriteLine("请输入第" + (i + 1) + "个人的数学成绩:"); } if (j == 2) { Console.WriteLine("请输入第" + (i + 1) + "个人的英语成绩:"); } shuzu[i, j] = double.Parse(Console.ReadLine()); } } Console.ReadLine();
//用二维数组输入“企”
string[,] n = new string[9, 9] { {" "," "," "," ","*"," "," "," "," "}, {" "," "," ","*"," ","*"," "," "," "}, {" "," ","*"," ","*"," ","*"," "," "}, {" ","*"," "," ","*"," "," ","*"," "}, {"*"," "," "," ","*","*","*"," ","*"}, {" "," "," "," ","*"," "," "," "," "}, {" "," ","*"," ","*"," "," "," "," "}, {" "," ","*"," ","*"," "," "," "," "}, {"*","*","*","*","*","*","*","*","*"} }; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { Console.Write(n[i, j]); } Console.WriteLine(); } Console.ReadLine();