C# 数组
数组:具有相同类型的若干变量按有序的形式组织起来的一种形式。这些按序排列的同类数据元素的集合称为数组。
定义数组
int[] 变量名 = new int [n];
string[] myStringArray = new string[6];
int[] myArray = new int[] {1, 3, 5, 7, 9};
取值:int a = myArray[0];
一维数组
int [] array = new int [5]{1,2,3,4,5};
int [] array = new int[5];
array[0] = 1;
array[1] = 2;
array[2] = 3;
array[3] = 4;
array[4] = 5;
// array[5] = 6; 超出索引范围
int a = array[2];
string [] ss = new string[3];
ss[0] = "1";
ss[1] = "2";
ss[2] = "3";
string b = ss[2];
输入三个人名放入数组
string[] name = new string[3];
//Console.Write("请输入第一个人名:");
//name[0] = Console.ReadLine();
//Console.Write("请输入第二个人名:");
//name[1] = Console.ReadLine();
//Console.Write("请输入第三个人名:");
//name[2] = Console.ReadLine();
for (int i = 1; i <= 3; i++)
{
Console.Write("请输入第{0}个人名:",i);
name[i - 1] = Console.ReadLine();
}
//根据班级人数创建一个数组,要求每个人的姓名都要放进去
Console.Write("请输入班级人数:");
int n = int.Parse(Console.ReadLine());
string [] name =new string[n];
for (int i = 0; i < n; i++)
{
Console.Write("请输入第{0}个人的姓名:",i+1);
name[i] = Console.ReadLine();
}
Console.WriteLine("所有人员姓名输入完毕,请按回车键查看所有人员姓名!");
Console.ReadLine();
Console.WriteLine();
for (int i = 0; i < n; i++)
{
Console.Write(name[i]+"\t");
}
Console.ReadLine();
从控制台输入班级人数
将每个人的年龄放入数组,
将所有人的年龄求总和
求平均年龄
求年龄最大
Console.Write("请输入班级人数:");
int n = int.Parse(Console.ReadLine());
int [] age = new int[n];
int sum = 0;
for (int i = 0; i < n; i++)
{
Console.Write("请输入第{0}个人的年龄:",i+1);
age[i] = int.Parse(Console.ReadLine());
sum+=age[i];
}
Console.WriteLine(sum);
Console.WriteLine(sum/n);
int agemax = 0;
for (int i = 0; i < n; i++)
{
if (agemax < age[i])
{
agemax = age[i];
}
}
Console.WriteLine("最大年龄是:"+agemax);
//冒泡排序
for (int i = 0; i < n; i++)
{
for (int j = i; j < n-1; j++)
{
if (age[i] < age[j+1])
{
int zhong = age[i];
age[i] = age[j+1];
age[j+1] = zhong;
}
}
}
for (int i = 0; i < n; i++)
{
Console.WriteLine(age[i]);
}
Console.ReadLine();
课后题
双色球:从33个红球中随机六个,按从小到大顺序排序,从16个蓝球中选取一个。
int[] suiji = new int[6];
Random ran = new Random();
for (int i = 0; i < 6; i++)
{
bool b = true;
int a = ran.Next(1, 34);
for (int j = 0; j < 1; j++)
{
if(a==suiji[j])
{
b=false;
i--;
}
}
if(b)
{
suiji[i]=a;
}
}
Console.Write("红球:");
for(int i = 0; i < 6; i++)
{
Console.Write(" "+suiji[i]);
}
Console.WriteLine();
for (int a = 0; a < 1; a++)
{
Random b = new Random();
int c = ran.Next(1,17);
Console.WriteLine("蓝色:"+c);
}
int[] shu = new int[] { 1,2,3};
二维数组
int [,] array = new int[4,2];
4,表示有四个一维数组
2,表示每一个一维数组有2个元素
int[,] shuzu = new int[,]
{
{1,2},
{3,4},
{5,6},
{7,8}
};
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 2; j++)
{
Console.Write(shuzu[i,j]+"\t");
}
}
打印出来一个“王”这个字
string[,] wang = new string[,]
{
{" ","■","■","■","■","■"," "},
{" "," "," ","■"," "," "," "},
{" "," "," ","■"," "," "," "},
{" "," ","■","■","■"," "," "},
{" "," "," ","■"," "," "," "},
{" "," "," ","■"," "," "," "},
{"■","■","■","■","■","■","■"}
};
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 7; j++)
{
Console.Write(wang[i,j]);
}
Console.WriteLine();
}
请输入班级人数,要求输入每个人的语数英成绩进入数组
求语文总分,数学的平均分,英语的最高分
打印出每个人的语数英成绩
Console.Write("请输入班级人数:");
int n = int.Parse(Console.ReadLine());
double[,] score = new double[n, 3];
for (int i = 0; i < n; i++)
{
Console.Write("请输入第{0}个人的语文成绩:",i+1);
score[i, 0] = double.Parse(Console.ReadLine());
Console.Write("请输入第{0}个人的数学成绩:", i + 1);
score[i, 1] = double.Parse(Console.ReadLine());
Console.Write("请输入第{0}个人的英语成绩:", i + 1);
score[i, 2] = double.Parse(Console.ReadLine());
}
Console.WriteLine("所有人的语数英成绩输入完毕,请按回车键打印!");
Console.ReadLine();
for (int i = 0; i < n; i++)
{
Console.WriteLine("第{0}个人的语文成绩是{1},数学成绩{2},英语成绩{3}。",i+1,score[i,0],score[i,1],score[i,2]);
}
求语文总分
double sum = 0;
for (int i = 0; i < n; i++)
{
sum += score[i, 0];
}
Console.WriteLine(sum);
Console.ReadLine();
多维数组
int[, ,] shuzu = new int[4, 3, 2];
4,表示有四个二维数组
3,表示每个二维数组里面有3个1维数组
2,表示每个1维数组里面有2个元素
string[, ,] gao = new string[3, 4, 1]
{
{
{"张三"},{"李四"},{"王五"},{"赵六"}
},
{
{"冯七"},{"王八"},{"张全蛋"},{"小沈阳"}
},
{
{"刘能"},{"赵四"},{"赵本山"},{"岳云鹏"}
}
};
split 分离
一次性接收姓名、年龄、工作单位
Console.Write("请输入您的姓名、年龄、工作单位(**-**-**):");
string quan = Console.ReadLine();
string[] array = quan.Split('-');
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine(array[i]);
}
Console.ReadLine();