C#语言基础——数组

时间:2022-03-25 23:38:19

数组

一、一位数组

数组初始化,创建数组,数组长度为5

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();

例题:1、输入班级人数,输入每个人的分数,求平均分

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();

2、从控制台输入班级人数,每个人的年龄放入数组,将所有的年龄求总和

Console.Write("输入班级人数:");

int a = int.Parse(Console.ReadLine());

int[] b = new int[a];

for (int i = 0; i < a; i++)

{

Console.Write("输入第" + (i + 1) + "个人的年龄");

b[i] = int.Parse(Console.ReadLine());

}

int sum = 0;

for (int i = 0; i < a; i++)

{

sum += b[i];

}

Console.WriteLine("年龄总和:" + sum);

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 } };

例题:1、根据二维数组,打印一个“王”

string[,] wang = new string[7, 7]

{

{"  ","★","★","★","★","★",""},

{"  ","  ","  ","★","","",""},

{"  ","  ","  ","★","","",""},

{"  ","  ","★","★","★","",""},

{"  ","  ","  ","★","","",""},

{"  ","  ","  ","★","","",""},

{"★","★","★","★","★","★","★"},

};

//打印出王

for (int i = 0; i < 7; i++)

{

for (int j = 0; j < 7; j++)

{

Console.Write(wang[i,j]);

}

Console.WriteLine();

}

Console.ReadLine();

2、输入班级人数,存放每个人的语、数、英成绩进入数组,求每个人的总分,求班级语文总分,数学平均分

法一、

Console.Write("输入班级人数:");

int a = int.Parse(Console.ReadLine());

double[,] fen = new double[a, 3];

for (int i = 0; i < a; i++)

{

Console.Write("输入第" + (i + 1) + "个人的语文分数");

fen[i, 0] = double.Parse(Console.ReadLine());

Console.Write("输入第" + (i + 1) + "个人的数学分数");

fen[i, 1] = double.Parse(Console.ReadLine());

Console.Write("输入第" + (i + 1) + "个人的英语分数");

fen[i, 2] = double.Parse(Console.ReadLine());

}

double sum = 0, sum1 = 0, sum2 = 0;

for (int i = 0; i < a; i++)

{

sum = 0;

for (int j = 0; j < 3; j++)

{

sum += fen[i, j];

}

sum1 += fen[i, 0];

sum2 += fen[i, 1];

Console.WriteLine("第" + (i + 1) + "个人的分数总和是" + sum);

}

Console.WriteLine("语文总和是" + sum1);

Console.WriteLine("数学平均分是" + (sum2 / a));

Console.ReadLine();

法二、

Console.Write("输入班级人数:");

int a = int.Parse(Console.ReadLine());

double[,] fen = new double[a, 3];

for (int i = 0; i < a; i++)

{

Console.Write("输入第" + (i + 1) + "个人的语文分数");

fen[i, 0] = double.Parse(Console.ReadLine());

Console.Write("输入第" + (i + 1) + "个人的数学分数");

fen[i, 1] = double.Parse(Console.ReadLine());

Console.Write("输入第" + (i + 1) + "个人的英语分数");

fen[i, 2] = double.Parse(Console.ReadLine());

}

double[] zong = new double[a];

for (int i = 0; i < a; i++)

{

zong[i] = fen[i, 0] + fen[i, 1] + fen[i, 2];

Console.WriteLine("第{0}个人的总分是:{1}", (i + 1), zong[i]);

}

double yuzong = 0, shuzong = 0;

for (int i = 0; i < a; i++)

{

yuzong += fen[i, 0];

shuzong += fen[i, 1];

}

Console.WriteLine("语文总分是:" + yuzong);

Console.WriteLine("数学平均分是:" + (shuzong / a));

Console.ReadLine();

三、冒泡排序

例题:1、从控制台输入班级人数,每个人的年龄放入数组,冒泡排序,求年龄最大。

Console.Write("输入班级人数:");

int a = int.Parse(Console.ReadLine());

int[] b = new int[a];

for (int i = 0; i < a; i++)

{

Console.Write("输入第" + (i + 1) + "个人的年龄");

b[i] = int.Parse(Console.ReadLine());

}

冒泡排序:固定格式,记住,背过

for (int i = 0; i < a; i++)

{

for (int j = i; j < a - 1; j++)

{

if (b[i] < b[j + 1])//从大到小

{

int zhong = b[i];

b[i] = b[j + 1];

b[j + 1] = zhong;

}

}

}

for (int i = 0; i < a; i++)

{

Console.WriteLine(b[i]);

}

Console.ReadLine();

2、从1-100内随机选10个不同的数做一个数组,对10个数排序,从小到大

法一、

int[] ss = new int[10];

Random s = new Random();

for (int i = 0; i < 10; i++)

{

ss[i] = s.Next(1, 100);

for (int j = 0; j < i; j++)

{

if (ss[i] == ss[j])

{

i--;

}

}

}

for (int i = 0; i < 10; i++)

{

Console.WriteLine(ss[i]);

}

Console.WriteLine();

for (int i = 0; i < 10; i++)

{

for (int j = i; j < 9; j++)

{

if (ss[i] > ss[j + 1])//从大到小

{

int zhong = ss[i];

ss[i] = ss[j + 1];

ss[j + 1] = zhong;

}

}

}

for (int i = 0; i < 10; i++)

{

Console.WriteLine(ss[i]);

}

Console.ReadLine();

法二、

int[] ss = new int[10];

Random s = new Random();

for (int i = 0; i < 10; i++)

{

bool b = true;

int a = s.Next(101);

for (int j = 0; j < i; j++)

{

if (a == ss[j])

{

b = false;

i--;

}

}

if (b)

{

ss[i] = a;

}

Console.WriteLine(ss[i]);

}

Console.WriteLine();

for (int i = 0; i < 10; i++)

{

for (int j = i; j < 9; j++)

{

if (ss[i] > ss[j + 1])//从大到小

{

int zhong = ss[i];

ss[i] = ss[j + 1];

ss[j + 1] = zhong;

}

}

}

for (int i = 0; i < 10; i++)

{

Console.WriteLine(ss[i]);

}

Console.ReadLine();

四、抽奖问题

输入每个人的中奖号码,进行滚动显示    //清屏  //随即一个索引   // 根据索引打印  //等待0.1秒

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();

}

Console.WriteLine("所有人的手机号码输入完毕。请按回车键!");

Console.ReadLine();

Random ran = new Random();

for (int i = 0; i < 30; i++)

{

Console.Clear();

int aa = ran.Next(a);

Console.WriteLine("中奖号码:"+jiang[aa]);

System.Threading.Thread.Sleep(100);//等待100毫秒

}

//作弊!!!!!!

//Console.Clear();

//Console.WriteLine("中奖号码:" + 14351571);

Console.ReadLine();