C#整理5——break与continue.及数组

一.break与continue.
这两个关键字一般放在循环的花括号里面使用。
break——结束整个循环。
continue——结束本次循环,进入下次循环。

break的案例:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 
 5 namespace ConsoleApplication1
 6 {
 7     class Program
 8     {
 9         static void Main(string[] args)
10         {
11             int i = 1;
12             for (; ; )
13             {
14                 if (i > 100)
15                 {
16                     break;
17                 }
18                 Console.Write(i + "\t");
19                 i++;
20             }
21 
22         }
23     }
24 }
View Code

continue的案例:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 
 5 namespace ConsoleApplication1
 6 {
 7     class Program
 8     {
 9         static void Main(string[] args)
10         {
11             for (int i = 1; i <= 100; i++)
12             {
13                 if (i % 2 == 0)
14                 {
15                     continue;
16                 }
17                 Console.Write(i + "\t");
18             }
19 
20         }
21     }
22 }
View Code

二.数组

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

一、一维数组:豆角。
定义:指定类型,指定长度,指定名称。
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.用循环来控制数组的下标,可以对数组进行批量操作。
例如:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 
 5 namespace ConsoleApplication1
 6 {
 7     class Program
 8     {
 9         static void Main(string[] args)
10         {
11             int[] a = new int[5];
12             //数组的批量赋值
13             for (int i = 0; i < 5; i++)
14             {
15                 a[i] = (i + 1) * 10;
16             }
17             //数组的批量取值。
18             for (int j = 0; j < 5; j++)
19             {
20                 Console.WriteLine(a[j]); //0下标。
21             }
22 
23         }
24     }
25 }
View Code

 

案例一:做一个教练为6个球员打分的程序。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 
 5 namespace ConsoleApplication1
 6 {
 7     class Program
 8     {
 9         static void Main(string[] args)
10         {
11             //定义一个保存球员成绩的数组
12             int[] a = new int[6];
13 
14             //输入
15             for (int i = 0; i < a.Length; i++)
16             {
17                 Console.Write("请输入第" + (i + 1) + "个球员的成绩:");
18                 a[i] = Convert.ToInt32(Console.ReadLine());
19             }
20 
21             //输出
22             for (int j = 0; j < a.Length; j++)
23             {
24                 Console.WriteLine("" + (j + 1) + "位球员的分数是" + a[j] + "分。");
25             }
26 
27         }
28     }
29 }
View Code

 

案例二:在案例一的基础上,显示球员总分和平均分。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 
 5 namespace ConsoleApplication1
 6 {
 7     class Class2
 8     {
 9         static void Main(string[] args)
10         {
11             int[] a = new int[6];
12 
13             Console.WriteLine("********球员训练记录********");
14 
15             //输入
16             for (int i = 0; i < a.Length; i++)
17             {
18                 Console.Write("请输入第"+(i+1)+"个球员的成绩:");
19                 a[i] = Convert.ToInt32(Console.ReadLine());
20             }
21             
22             //输出每个球员的分
23             for(int j=0;j<a.Length;j++)
24             {
25                 Console.WriteLine(""+(j+1)+"位球员的分数是"+a[j]+"分。");
26             }
27             //计算并显示总分和平均分。
28             int sum = 0;
29             for(int i=0;i<a.Length;i++)
30             {
31                 sum = sum + a[i];
32             }
33             double avg = 0;
34 
35             avg = 1.0 * sum / a.Length;
36             Console.WriteLine("总分是:" + sum + "。平均分是:" + avg + "");
37 
38         }
39     }
40 }
View Code

 


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

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Class3
 9     {
10         static void Main(string[] args)
11         {
12             int[] a = new int[6];
13             //输入
14             for(int i=0;i<a.Length;i++)
15             {
16                 Console.Write("请输入第"+(i+1)+"个球员的分数:");
17                 a[i] = Convert.ToInt32(Console.ReadLine());
18             }
19 
20             //找最大和最小
21             int max = 0, min = 100000;
22             int maxSub = -1, minSub = -1;
23             for(int i=0;i<a.Length;i++)
24             {
25                 if(a[i]>max)
26                 {
27                     max = a[i];
28                     maxSub = i;
29                 }
30                
31            
32                 if (a[i] < min)
33                 {
34                     min = a[i];
35                     minSub = i;
36                 }
37             }
38             //输出
39             maxSub++;
40             minSub++;
41             Console.WriteLine(maxSub+"号球员分数最高,分数是:"+max+""+minSub+"号球员分数最低,分数是:"+min);
42         }
43     }
44 }
View Code

 

案例四:青歌赛中有10个评委给一个选手打分,每打分后,要去掉一个最高分和一个最低分,计算该选手的平均得分。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             int[] a = new int[10];
13             for (int t = 0; t < 10; t++)
14             {
15                 Console.Write("请为选手打分:");
16                 a[t] = Convert.ToInt32(Console.ReadLine());
17                 for (int i = 1; i <= a.Length - 1; i++)
18                 {
19                     for (int j = 1; j <= a.Length - i; j++)
20                     {
21                         if (a[j - 1] < a[j])
22                         {
23                             int mm = a[j];
24                             a[j] = a[j - 1];
25                             a[j - 1] = mm;
26 
27                         }
28                     }
29                 }
30             }
31             int sum = 0;
32             for (int p = 2; p < a.Length - 2; p++)
33             {
34 
35                 sum = sum + a[p];
36             }
37             Console.Write("选手得分是" + 1.0 * sum / 6);
38         }
39     }
40 }
View Code

 

案例五:做一个36选7的彩票生成器。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Class4
 9     {
10         static void Main(string[] args)
11         {
12             int[] a = new int[7];
13 
14             Random rand = new Random();
15             for (int i = 0; i < 7; i++)    //7--代表要生成7个不同的数
16             {
17                 //生成一个随机数。
18                 int n = rand.Next(36);
19                 n++;
20                 
21                 //查重
22                 bool chong = false;
23                 for(int j=0;j<a.Length;j++)
24                 {
25                     if(n == a[j])
26                     {
27                         chong = true;
28                         break;
29                     }
30                 }
31                 //才能确定n合不合理
32                 if(chong == false)
33                 {
34                     a[i] = n;
35                 }
36                 else
37                 {
38                     i--;
39                 }//if
40             }//for
41 
42             //显示彩票号码
43             for(int k = 0;k<a.Length;k++)
44             {
45                 Console.Write(a[k] + "\t");
46             }
47         }//main
48     }
49 }
View Code

 

案例六 20个手机号 滚动显示,随机抽取一个中奖号码

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             string[] cellPhone = new string[] { "13012345678", "13109876543", "13287654678", 
13                 "13309876789", "13509878902", "13698374651", "13757893421", "13876561234", 
14                 "13909876543", "15034567438", "15111234795", "15894574839", "18210394857", 
15                 "18302938475" };
16 
17             Random rand = new Random();
18             for(int i=0;i<50;i++)
19             {
20                 //变慢一些。
21                 System.Threading.Thread.Sleep(100);
22                 //随机生成数组的下标。
23                 int sub = rand.Next(cellPhone.Length);
24                 //根据下标取数组的元素值。
25                 string s = cellPhone[sub];
26                 //显示
27                 Console.Clear();
28                 Console.WriteLine(s);
29             }
30         }
31     }
32 }
View Code

 

案例7 选班长 30个同学投票,从5个候选人中选出一个班长

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Class1
 9     {
10         static void Main(string[] args)
11         {
12             //30人投票,从5个候选人选一个出来。
13             int[] vote = new int[5];
14             for(int i=0;i<30;i++)
15             {
16                 Console.Write("请第"+(i+1)+"位同学投票(0-4):");
17                 int temp = Convert.ToInt32(Console.ReadLine());
18                 if(temp <0 || temp >4)
19                 {
20                     Console.WriteLine("废票");
21                     continue;
22                 }
23                 else
24                 {
25                     vote[temp]++;
26                 }
27             }
28 
29             //计算最终得票。
30             int max = 0, maxSub = 0;
31             for(int i=0;i<vote.Length;i++)
32             {
33                 //把每位候选人的票数显示出来。
34                 Console.WriteLine("" + (i + 1) + "号候选人的票数是:" + vote[i]); 
35                 //计算最大值。
36                 if(vote[i] > max)
37                 {
38                     max = vote[i];
39                     maxSub = i;
40                 }
41             }
42 
43             //显示最终结果。
44             Console.WriteLine("最终投票结果为:"+(maxSub+1)+"号候选人当选,当选票数是"+max+"票。");
45         }
46     }
47 }
View Code

 

posted on 2015-03-05 15:37  酸甜sky  阅读(397)  评论(0编辑  收藏  举报