标签:ar sp for strong on 2014 问题 bs ad
数组:定义一组同类型的指定个数的变量,索引从0开始
int [] shuzuming=new int[10]; //中括号中为需要定义的长度(个数)
shuzuming[0]=1;
shuzuming[1]=2;
//彩票双色球问题(红球33选6,蓝球16选1)
int[] hongqiu=new int[6];
Random r=new Random();
for(int i=0;i<6;i++)
{
hongqiu[i] = r.Next(1, 34);
for (int j = 0; j < i; j++)
{
if (hongqiu[j] == hongqiu[i])
{
i--; //如果第j个红球等于第i个红球的话,跳出循环,让第j个红球与第i-1求红球比较
break;
}
}
}
//输入10个分数,求总分和平均分
int[] fenshu = new int[10];
for (int i = 0; i <10; i++)
{
Console.WriteLine("请输入第{0}个分数", i+1);
fenshu[i] = int.Parse(Console.ReadLine());
}
int sum = 0;
for (int j = 0; j < 10; j++)
{
sum = sum + fenshu[j];
}
double avg=sum/fenshu.Length;
Console.WriteLine("总分为{0}", sum);
Console.WriteLine("平均分为{0}", avg);
Console.ReadLine();
//求最高分最低分
int max = 0;
int min = 0;
for (int k = 0; k <= 10; k++)
{
if (max < fenshu[k])
{
max = fenshu[k];
}
if (min > fenshu[k])
{
min = fenshu[k];
}
}
Console.WriteLine("最高分为{0}", max);
Console.WriteLine("最低分为{0}", min);
foreach:
foreach(int a in shu):会自动读取数组中的每一个数,然后把值赋值给a
foreach(int a in fenshu)
{
Console.WriteLine(a);
}
foreach其实相当于 for(int i=0;i<=shu.length;i++)
{
int a;
a=shu[i];
console.writeline(a);
}
等量代换:需要中间变量
int a = 3, b = 5;
int c;
c = a;
a = b;
b = c;
冒泡排序:
for (int m = 0; m < 10 - 1; m++)
{
for (int n = m; n < 10; n++)
{
if (fenshu[m] < fenshu[n])
{
int zhong;
zhong = fenshu[m];
fenshu[m] = fenshu[n];
fenshu[n] = zhong;
}
}
}
标签:ar sp for strong on 2014 问题 bs ad
原文地址:http://www.cnblogs.com/jintuo/p/4164593.html