数组
数组,就是相同数据类型的元素按一定顺序排列的集合,就是把有限个类型相同的变量用一个名字命名,然后用编号区分他们的变量的集合,这个名字称为数组名,编号称为下标。
一维数组
数组初始化,创建数组,数组长度为5
数组中元素的索引是从0开始的
int [] array=new int[5];
array[0] = 1;
array[1] = 2;
array[2] = 3;
array[3] = 4;
array[4] = 5;
//将2号索引位置改成7
array[2] = 7;
//想查看3号索引位置是什么值
Console.WriteLine(array[3]);
//把4号索引位置的值赋值给a
int a = array[4];
//将1号索引位置的值变成从控制台输入
array[1] = int.Parse(Console.ReadLine());
Console.ReadLine();
//直接赋值的数组
int[] array1 = new int[] {1,2,3,4,5 };
例: 存放5个人的人名
string [] name =new string[5];
for (int i = 1; i <= 5; i++)
{
Console.Write("请输入第"+i+"个人的名字:");
name[i-1] = Console.ReadLine();
}
//查看第三个人的姓名
//Console.WriteLine(name[2]);
//将每个人的姓名都打印出来
for (int i = 0; i < 5; i++)
{
Console.WriteLine(name[i]);
}
Console.ReadLine();
例:输入班级人数,输入每个人的分数,求平均分
Console.Write("请输入班级人数:");
int a = int.Parse(Console.ReadLine());
double[] fen = new double[a];
for (int i = 0; i < a; i++)
{
Console.Write("请输入第" + (i + 1) + "个人的分数:");
fen[i] = double.Parse(Console.ReadLine());
}
double sum = 0;
for (int i = 0; i < a; i++)
{
sum += fen[i];
}
Console.WriteLine("平均分是:" + (sum / a));
Console.ReadLine();
二维数组
int [,] shuzu = new int[3,4];
//3 表示有3个一维数组
//4 表示每一个一维数组有4个元素
//给第一个一维数组的2号索引赋值
shuzu[0, 2] = 3;
//直接赋值
int[,] shuzu1 = new int[,] {
{1,2,3,4},
{2,3,4,5},
{3,4,5,6}
};
例: 输入班级人数,存放每个人的语、数、英成绩进入数组
Console.Write("请输入班级人数:");
int a = int.Parse(Console.ReadLine());
double [,] score = new double [a,3];
for (int i = 0; i < a; 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());
}
//求每个人的总分
double [] zong = new double[a];
for (int i = 0; i < a; i++)
{
zong[i] = score[i, 0] + score[i, 1] + score[i, 2];
Console.WriteLine("第{0}个人的总分是:{1}",(i+1),zong[i]);
}
//求语文总分 //求数学平均分
double yuzong = 0;
double shuzong = 0;
for (int i = 0; i < a; i++)
{
yuzong += score[i, 0];
shuzong += score[i, 1];
}
Console.WriteLine("语文总分是:"+yuzong);
Console.WriteLine("数学平均分是:"+shuzong/a);
Console.ReadLine();
例:根据二维数组,打印一个“王”
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.ReadLine();
多维数组:
数组可以具有多个维度。例如:
int[,,] myArray = new int[2,4,2] {{1,2},{3,4},{5,6},{7,8}} , {{9,10},{11,12},{13,14},{15,16}};
//2 表示有2个二维数组
//4 表示 每个二位数组里面有4个一维数组
//2 表示 每一个一维数组里面有几个元素
冒泡排序
数组当中不自带排序的方法,所以需进行冒泡排序
拿第一个值和第二个值比,若第一个数小于第二个数进行调换,然后第一个值再和第三个值比较,若小于再调换
例:33 22 11 66 44
33 22 11 66 44
33 22 11 66 44
66 22 11 33 44
66 33 11 22 44
66 44 11 22 33
66 44 22 11 33
66 44 33 11 22
66 44 33 22 11
例:从控制台输入班级人数,每个人的年龄放入数组,
将所有人的年龄求总和,冒泡排序,求年龄最大
Console.Write("请输入班级人数:");
int a = int.Parse(Console.ReadLine());
int[] age = new int[a];
//int sum = 0;
for (int i = 0; i < a; i++)
{
Console.Write("请输入第{0}个人的年龄:", (i + 1));
age[i] = int.Parse(Console.ReadLine());
//sum += age[i];
}
//Console.WriteLine("年龄总和是:" + sum);
//冒泡排序
for (int i = 0; i < a; i++)
{
for (int j = i; j < a - 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 < a; i++)
{
Console.WriteLine(age[i]);
}
Console.ReadLine();
例: 随机生成10个100以内的不重复的整数放入数组,打印
然后排序,要求,从小到大
int[] suiji = new int[10];
Random ran = new Random();
for (int i = 0; i < 10; i++)
{
bool b = true;
int a = ran.Next(11);
for (int j = 0; j < i; j++)
{
if (a == suiji[j])
{
b = false;
i--;
j = 100;
}
}
if (b)
{
suiji[i] = a;
}
}
//打印每一个数
for (int i = 0; i < 10; i++)
{
Console.WriteLine(suiji[i]);
}
Console.WriteLine();
//冒泡排序
for (int i = 0; i < 10; i++)
{
for (int j = i; j < 9; j++)
{
if (suiji[i] > suiji[j + 1])
{
int zhong = suiji[i];
suiji[i] = suiji[j + 1];
suiji[j + 1] = zhong;
}
}
}
//打印每一个数
for (int i = 0; i < 10; i++)
{
Console.WriteLine(suiji[i]);
}
Console.ReadLine();
例: // 双色球随机出号码
// 6个红色球,1个蓝色球
// 红色球号码不重复,从小到大排列,打印的时候拼上蓝色球
int[] suiji = new int[6];
Random ran = new Random();
int x = ran.Next(1, 17);
for (int i = 0; i < 6; i++)
{
bool b = true;
int a = ran.Next(1, 33);
for (int j = 0; j < i; j++)
{
if (a == suiji[j])
{
b = false;
i--;
}
}
if (b)
{
suiji[i] = a;
}
}
for (int i = 0; i < 6; i++)
{
for (int j = i; j < 5; j++)
{
if (suiji[i] > suiji[j+1])
{
int zhong = suiji[i];
suiji[i] = suiji[j+1];
suiji[j+1] = zhong;
}
}
}
for (int i = 0; i < 6; i++)
{
Console.Write(suiji[i] + "\t");
}
Console.Write(x);
Console.ReadLine();
//输入手机号码,做滚动抽奖
Console.Write("请输入手机号码");
int a = int.Parse(Console.ReadLine());
string[] jiang = new string[a];
for (int i = 0; i < a; i++)
{
Console.Write("输入第{0}个人的手机号", (i + 1));
jiang[i] = Console.ReadLine();
}
Random ran = new Random();
Console.Write("所有号码输入完毕");
for (int i = 0; i < 30; i++)
{
Console.Clear();
int aa = ran.Next(a);
Console.WriteLine("号码是" + jiang[aa]);
System.Threading.Thread.Sleep(100);
}
Console.ReadLine();