定义数组 int[] 变量名 = new int [n];
数组初始化
int[] myArray = new int[] {1, 3, 5, 7, 9};
//输入班级人数
//输入每个人的分数
//求总分、平均分、两个最高分,两个最低分
Console.Write("请输入班级的人数");
int n = int.Parse(Console.ReadLine());
double[] b = new double[n];
double fen = 0;
for (int i = 0; i < n; i++)
{
Console.Write("请输入{0}的分数:", i + 1);
b[i] = double.Parse(Console.ReadLine());
fen += b[i];
}
Console.Write(fen);
Console.Write(fen/n);
Console.ReadLine();
练习:双色球抽奖
int[] red = new int[6];
Random ran = new Random();
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 == red[j])
{
b = false;
i--;
j = 1000;
}
}
if (b == true)
{
red[i] = a;
}
}
for (int i = 0; i <6; i++)
{
for (int j = i; j <5; j++)
{
if (red[i] > red[j+1])
{
int zhong = red[i];
red[i] = red[j+ 1];
red[j+1] = zhong;
}
}
}
int s = ran.Next(16);
for (int i = 1; i < 34; i++) //滚动显示
{
Console.Clear();//清屏
int a = ran.Next(1,33);
int b = ran.Next(1,33);
int c = ran.Next(1,33);
int d = ran.Next(1,33);
int e = ran.Next(1,33);
int f = ran.Next(1,33);
int g = ran.Next(1,16);
Console.WriteLine("中奖号码:"+a + "\0" + b + "\0" + c + "\0" + d + "\0" + e + "\0" + f + "\0"+g);
System.Threading.Thread.Sleep(100);//暂停100毫秒,0.1秒
}
////作弊代码
Console.Clear();
Console.WriteLine("中奖号码:" + red[0] + "\0" + red[1] + "\0" + red[2] + "\0" +red[3] + "\0" + red[4] + "\0" +red[5] + "\0" + s);
Console.ReadLine();