3月13日 冒泡排序
//冒泡排序 从大到小排列
1. //int []shuzu = new int[]{3,1,8,4,0,5,6};
int []shuzu = new int[]{3,1,8,4,0,5,6}; int zhong; for (int i = 0; i < 7; i++) { for (int j = i; j < 6; j++) { if (shuzu[i] <shuzu[j + 1]) { zhong = shuzu[i]; shuzu[i] = shuzu[j + 1]; shuzu[j + 1] = zhong; } } } //for (int i = 1; i < 7; i++) //{ // Console.WriteLine(shuzu[i]); //} foreach (int a in shuzu) { Console.WriteLine(a); } Console.ReadLine();
foreach(int a in shuzu) //遍历数组 int也可以用object
{
/Console.WriteLine(a);
}
object是所有类的基类,可以接收任何数据类型
eg: //object a = 1;
//object b = "123";
//object c = true;
//object d = 3.14;
2. //输入人数,输入每个人的成绩
//需要最高分、最低分
//去掉两个最高分,去掉两个最低分,求平均分。
Console.Write("请输入人数:"); int ren = int.Parse(Console.ReadLine()); double[] chengji = new double[ren]; if (ren >= 5) { for (int i = 0; i < ren; i++) { Console.Write("请输入第" + (i + 1) + "个人的成绩:"); chengji[i] = double.Parse(Console.ReadLine()); } double zhong; for (int i = 0; i < ren; i++) { for (int j = i; j < ren - 1; j++) { if (chengji[i] < chengji[j + 1]) { zhong = chengji[i]; chengji[i] = chengji[j + 1]; chengji[j + 1] = zhong; } } } foreach (double a in chengji) { Console.WriteLine(a); } double sum = 0; for (int i = 0; i < ren; i++) { sum += chengji[i]; } double pj = (sum - chengji[0] - chengji[1] - chengji[ren - 1] - chengji[ren - 2]) / (ren - 4); Console.WriteLine("去掉两个最高分" + chengji[0] + "、" + chengji[1] + ",去掉两个最低分" + chengji[ren - 2] + "、" + chengji[ren - 1], ""); Console.WriteLine("他们的平均分是:" + pj); } else { Console.WriteLine("您的输入有误!"); } Console.ReadLine();
这个是复习昨天内容的练习题
随机输入手机号码,滚动起来,从中抽取一个获奖,5秒后停止。
Console.Write("请输入手机号码数量:"); int a = int.Parse(Console.ReadLine()); string[] shuzu = new string[a]; if (a >= 5) { for (int i = 0; i < a; i++) { Console.Write("请输入第" + (i + 1) + "个手机号码:"); shuzu[i] = Console.ReadLine(); } Console.WriteLine("请按回车键开始滚动"); Console.ReadLine(); Random ran = new Random(); int j = 0; for (; ; j++) { int b = ran.Next(a); Console.Clear(); Console.WriteLine("中奖号码为:"+shuzu[b]); System.Threading.Thread.Sleep(100); if (j == 49) { break; } } } else { Console.WriteLine("您输入的手机号码数量太少!"); } Console.ReadLine();