C#(⼀维数组)
定义数组 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();
练习1:双⾊球抽奖
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();
练习2.  随机出现⼀个四位的验证码,打印出来
⽐照打出来,验证输⼊的验证码是否有误
验证码不区分⼤⼩写
Random ran = new Random();
string[] ss = new string[62] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "
e", "f", "g", "h", "i", " string[] s = new string[4];
for (int i = 0; i < 4; i++)
{
int a = ran.Next(0, 62);
s[i] = ss[a];
}
string b = s[0] + s[1] + s[2] + s[3];
Console.WriteLine("验证码:" + b);
Console.Write("请输⼊验证码:");
string shu = Console.ReadLine();
if (shu.ToLower() == b.ToLower())
{
Console.WriteLine("输⼊正确!");
}
else
{
Console.WriteLine("错误!");
}
Console.ReadLine();

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。