数组

一维数组  【相同数据类型】

int []shuzu=new int [5];
for (int i = 0; i <= 4; i++)
{
 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 };

 

从控制台输入10个人的年龄放入数组,并求出年龄和。

1             int[] age = new int[10];
2             int sum = 0;
3             for (int i = 0; i < 10; i++)
4             {
5                 age[i] = int.Parse(Console.ReadLine());
6                 sum += age[i];
7             }
8             Console.WriteLine(sum);

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

 1  Console.WriteLine("请输入班级人数:");
 2             int a = int.Parse(Console.ReadLine());
 3             double  [] b=new double  [a];
 4             double sum = 0;
 5             for (int i = 0; i < a; i++)
 6             {
 7                 Console.Write(""+(i+1)+"个人的成绩为:");
 8                 b[i] = int.Parse(Console.ReadLine());
 9                 sum += b[i];
10             }
11             Console.WriteLine("平均成绩为:"+sum / a);

 

 

二维数组

格式:int[,] shuzu = new int[3, 4];
int[,] shuzu = new int[,] {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 6, 7, 8, 9 }
};

利用二维数组输出一个“企”字

 1 string[,] m = new string[8, 7]{
 2             {"  ","  ","  ","","  ","  ","  "},
 3             {"  ","  ","","  ","","  ","  "},
 4             {"  ","","  ","","  ","","  "},
 5             {"","  ","  ","","  ","  ",""},
 6             {"  ","  ","  ","","","","  "},
 7             {"  ","","  ","","  ","  ","  "},
 8             {"  ","","  ","","  ","  ","  "},
 9             {"","","","","","",""}
10             };
11             for (int i = 0; i < 8; i++)
12             {
13                 for (int j = 0; j < 7; j++)
14                 {
15                     Console.Write(m[i, j]);
16                 }
17                 Console.WriteLine();
18             }

 

 

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

 1             Console.WriteLine("请输入班级人数:");
 2             int a = int.Parse(Console.ReadLine());
 3             double[,] shuzu = new double[a, 3];
 4             for (int i = 0; i < a; i++)
 5             {
 6                 for (int j = 0; j < 3; j++)
 7                 {
 8                     if (j == 0)
 9                     {
10                         Console.Write("" + (i + 1) + "个人的语文成绩为:");
11                     }
12                     if (j == 1)
13                     {
14                         Console.Write("" + (i + 1) + "个人的数学成绩为:");
15                     }
16                     if (j == 2)
17                     {
18                         Console.Write("" + (i + 1) + "个人的数学成绩为:");
19                     }
20                     shuzu[i, j] = double.Parse(Console.ReadLine());
21                 }
22             }

三维数组

int[, ,] shuzu = new int[3, 2, 4];

 

posted @ 2016-03-12 16:23  冷风~云  阅读(182)  评论(0编辑  收藏  举报