打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
c# break与continue运用,数组
二、1.break与continue.
这两个关键字一般放在循环的花括号里面使用。break——结束整个循环。continue——结束本次循环,进入下次循环。

break的案例:int i = 1;for(;;)
{if(i>100)
{break;
}
Console.Write(i+"\t");
i++;
}

continue的案例:for (int i = 1; i <= 100; i++)
{if(i%2 == 0)
{continue;
}
Console.Write(i + "\t");
}2.while循环//初始条件while(循环条件)
{//循环体//状态的改为}
案例:int i = 1;int count=0; //记录与7有关的数字的个数while(i<=100)
{if(i%7==0 || i%10==7||i/10==7)
{
Console.Write(i+"\t");
count++;//1}
i++;//2}//3Console.Write("共有"+count+"个与7相关的数");3.do...while(循环条件)简单了解。
即使初始条件不满足循环条件,循环还会执行一次。
至少执行一次。

数组:解决同一类大量数据在内存存储和运算的功能。
分类:一维数组、二维数组、多维数组。
特点:连续,同一类数据。

一、一维数组:豆角。
定义:指定类型,指定长度,指定名称。int[] a = new int[5]; //5是长度。从1开始算。默认5个元素初始值都是0.int[] a = new int[5] { 90, 95, 89, 76, 99 };int[] a = new int[5] { 90, 95, 89 }; //语法有错,后面初始化的值必须是5个。int[] a = new int[] { 90, 95, 89, 76, 99}; //计算机会根据后面的赋值,动态计算数组的长度。赋值:
数组名[下标数值] = 值;int[] a = new int[5];
a[0] = 10;
a[1] = 20;
a[2] = 30;
a[3] = 40;
a[4] = 50;

取值:
数组名[下标数值]; //下标数值从0开始。Console.WriteLine(a[3]+a[0]);

数组的好处:1.对于大量数据来说,保存的时候,定义一个数组即可解决。2.用循环来控制数组的下标,可以对数组进行批量操作。
例如:int[] a = new int[5];//数组的批量赋值for (int i = 0; i < 5;i++ )
{
a[i] = (i + 1) * 10;
}//数组的批量取值。for (int j = 0; j < 5;j++ )
{
Console.WriteLine(a[j]); //0下标。}

案例一:做一个教练为6个球员打分的程序。//定义一个保存球员成绩的数组int[] a = new int[6];//输入for (int i = 0; i < a.Length; i++)
{
Console.Write("请输入第"+(i+1)+"个球员的成绩:");
a[i] = Convert.ToInt32(Console.ReadLine());
}//输出for(int j=0;j<a.Length;j++)
{
Console.WriteLine("第"+(j+1)+"位球员的分数是"+a[j]+"分。");
}
案例二:在案例一的基础上,显示球员总分和平均分。


案例三:在案例二的基础上,显示最高分和最低分,以及相应球员的代号。

案例四:青歌赛中有10个评委给一个选手打分,每打分后,要去掉2个最高分和2个最低分,计算该选手的平均得分。//int[] a = new int[10];//for (int t = 0; t < 10; t++)//{// Console.Write("请为选手打分:");// a[t] = Convert.ToInt32(Console.ReadLine());// for (int i = 1; i <= a.Length - 1; i++)// {// for (int j = 1; j <= a.Length - i; j++)// {// if (a[j - 1] < a[j])// {// int mm = a[j];// a[j] = a[j - 1];// a[j - 1] = mm;// }// }// }//}// int sum = 0;// for (int p = 2; p < a.Length - 2; p++)// {// sum = sum + a[p];// }// Console.Write("选手得分是"+1.0*sum/6);案例五:做一个36选7的彩票生成器。//int[] a=new int[7];//Random rand = new Random();//for (int i = 0; i < 7; i++)//7 代表要生成7个不同的数//{//生成一个随机数// int n = rand.Next(36);// n++;//查重// bool chong = false;// for (int j = 0; j < a.Length; j++)// {// if (n == a[j])// {// chong = true;// break;// }// }//才能确定n合不合理// if (chong == false)// {// a[i] = n;// }// else// {// i--;// }//}//显示彩票号码// for(int k=0;k<a.Length ;k++)// {// Console .Write(a[k]+"\t");// }案例六 20个手机号 滚动显示,随机抽取一个中奖号码

方法一//string[] call = new string[] { n个手机号 };//Random rand = new Random();//for (int i = 0; i < 500; i++)//{// //随机生成数组的下表// System.Threading.Thread.Sleep(50);// int sub=rand.Next (call.Length) ;// //根据下表取数组的元素之// string s=call[sub];// Console .Clear();// Console .WriteLine(s);//}方法2//string[] a = new string[20];//Random rand = new Random();//for (int i = 0; i < a.Length; i++)//{// Console.Write("请输入第" + (i + 1) + "个号码:");// a[i] = (Console.ReadLine());//}// for (int i = 0; i < a.Length; i++)// {// System.Threading.Thread.Sleep(50);// int n = rand.Next(a.Length );// string s = a[n];// Console.Clear();// Console.Write("中奖号码是" + s);//}案例7 选班长 30个同学投票,从5个候选人中选出一个班长int[] vote = new int[5];

for (int i = 0; i < 30; i++)
{
Console.Write("第" + (i + 1) + "个同学投票的结果是(a-1,b-2,c-3.d-4,e-5):");int temp = Convert.ToInt32(Console.ReadLine());if (temp <0|| temp >4)
{
Console.Write("废票");continue;

}else{
vote[temp]++;
}



}int max=0,maxsub=0;for(int i=0;i<vote.Length ;i++)
{
Console .WriteLine("第"+(i+1)+"号候选人的票数是:"+vote[i]);if(vote[i]>max)
{
max=vote[i];
maxsub=i+1;
}
}
Console .WriteLine (maxsub+"号候选人当选");
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
c#中求最大公约数和最小公倍数
C#中foreach遍历
给数组扩容的几种方式
(教学思路 C#数组二)数组的属性、foreach遍历、交错数组与矩形数组的区别
C#指针操作Marshal实例
c#语法之ArryList
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服