一、順序語句
二、條件,分支語句
1、if語句
關鍵是能夠熟練運用 if的嵌套。要考慮好所有的情況。
如果說 條件是兩種情況相互對應的,那麼就可以只用 if 與else 。但必須要想好 每個else 跟哪個if是一對。
如果情況是相互獨立的三種情況以上,那麼可以選擇運用if ... else if ...else。
1.if語句
if(條件)
{
滿足條件的時候執行;
}
2. if(條件)
{
滿足條件執行;
}
else
{
不滿足條件時執行;
}
3 if(條件1)
{
滿足條件1的時候執行;
}
else if(條件2)
{
不滿足條件1的情況下滿足條件2;
}
4.
if(條件1)
{
if(條件2)
{
既滿足條件1又滿足條件2的時候執行;
}
}
2、switch 語句
如果說可選的條件比較多時,選擇switch語句,要比if語句效率要高。特別注意的是 case 後跟的break。
eg:
1 //eg.6 swtich語句 作用域
2 static void Maine(string[] args)
3 {
4 //Console.WriteLine("你本次選擇出場的英雄是:");
5 Random r = new Random();
6 int n = r.Next(10);
7
8 string a;
9
10 switch (n)
11 {
12 case 1:
13 a = "趙信"; break;
14 case 2:
15 a = "寒冰射手";break;
16 case 3:
17 a = "無極劍聖";break;
18 case 4:
19 a = "機器人"; break;
20 default:
21 a = "齊天大聖";break;
22 }
23 Console.WriteLine("本次選擇的英雄是:"+a);
24 }
三、循環語句
for循環
四要素:
初始條件,循環條件,狀態改變,循環體。 執行過程:
初始條件--循環條件--循環體--狀態改變--循環條件....
注意:for的小括號裡面分號隔開,for的小括號後不要加分號。
利用 加斷點的方式,可以更好的明白for的工作原理。
1.for循環空操作完成的實例, 輸出100以內的數字
1 static void Main(string[] args)
2 {
3 int i = 1;
4 for (; ; )
5 {
6 if (i > 100)
7 {
8 break;
9 }
10 Console.Write(i + "\t");
11 i++;
12 }
13 Console.ReadKey();
14 }
當然你也可以用 while,if() break;的嵌套完成上述操作。
2.正序和逆序的推斷問題。 (折紙問題)
1 //eg.5 折紙問題
2 static void Maine(string[] args)
3 {
4 //Console.WriteLine("請輸入次數");
5 //int n = Convert.ToInt32(Console.ReadLine());
6
7
8 //int i = 0;
9 //for (double sum = 0.0001; sum <= 8848.0; sum = sum * 2)
10 //{
11 // i++;
12
13 //}
14 //Console.WriteLine(i);
15
16 double sum = 0.0001;
17 int z = 0;
18
19 for (int i = 0; ; i++)
20 {
21 z++;
22 sum = sum * 2;
23
24 if (sum >= 8848.0)
25 {
26 Console.WriteLine(z);
27 break;
28 }
29 }
30 }
3.應用:a.窮舉法: 用循環把各種可能的情況都給走一遍,然後用if條件把滿足要求的結果給篩選出來。
1 //eg.6 百馬百石 大馬馱2石,中馬馱1石 小馬馱0.5石
2 static void Main6a(string[] args)
3 {
4 for (int i = 0; i <= 50; i++)
5 {
6 for (int j = 0; j <= 100; j++)
7 {
8 for (int k = 0; k <= 200; k++)
9 {
10 if ( (i * 2 + j * 1 + k * 0.5 == 100) && (i + j + k == 100) )
11 {
12 Thread.Sleep(50);
13 Console.WriteLine("大馬需要" + i + "頭,中馬需要" + j + "頭,小馬需要" + k + "頭。");
14 }
15 }
16 }
17 }
18 }
19 //eg.7
20 static void Maing(string[] args)
21 {
22 for (int i = 1; i < 10; i++)
23 {
24 for (int j = 1; j < 5; j++)
25 {
26 for (int k = 1; k < 25; k++)
27 {
28 if (i * 5 + j * 10 + k * 25 == 50)
29 {
30 Console.WriteLine("50元用來買" + i.ToString() + "個牙刷," + j.ToString() + "個牙膏," + k.ToString() + "塊肥皂,正好能用完。");
31 }
32 }
33 }
34 }
35
36 }
37 //eg.8 有1塊,2塊,5塊的錢若干,湊出20塊錢,有幾種湊法
38 static void Mainh(string[] args)
39 {
40 int m = 0;
41 for (int i = 0; i <= 20; i++)
42 {
43 for (int j = 0; j <= 10; j++)
44 {
45 for (int k = 0; k < 4; k++)
46 {
47 if (i * 1 + 2 * j + 5 * k == 20)
48 {
49 m++;
50 Console.WriteLine("一共有" + m + "中方法。");
51 Console.WriteLine("需要1元的" + i + "張,2元的" + j + "張,5元的" + k + "張。");
52 }
53 }
54 }
55 }
56 }
57 //eg.9 1 () 2 () 3 ()4 = 4;問括號裡我要填 (- 或 +)
58 static void Maini(string[] args)
59 {
60 for (int i = 1; i <= 1; i += 2)
61 {
62 for (int j = -1; j <= 1; j += 2)
63 {
64 for (int k = -1; k <= 1; k += 2)
65 {
66 for (int l = -1; l <= 1; l += 2)
67 {
68 if (1 * i + 2 * j + 3 * k + l * 4 == 4)
69 {
70 Console.WriteLine("i=" + i + ",j=" + j + ",k=" + k + ",l=" + l + "。");
71 }
72 }
73
74
75 }
76 }
77 }
78 }
79 //eg.10 123()45()67()8()9=100;要求在()裡面填寫+或-使等式成立。
80 static void Maini2(string[] args)
81 {
82 for (int a = -1; a <= 2; a += 2)
83 {
84 for (int b = -1; b <= 2; b += 2)
85 {
86 for (int c = -1; c <= 2; c += 2)
87 {
88 for (int d = -1; d <= 2; d += 2)
89 {
90 if (123 + a * 45 + b * 67 + c * 8 + d * 9 == 100)
91 Console.WriteLine("a=" + a + ",b=" + b + ",c=" + c + ",d=" + d);
92 }
93 }
94 }
95 }
96 Console.ReadKey();
97 }
98 //eg.11 某偵查隊接到一項緊急任務,要求在A.B.C,D,E,F六名隊員中盡可能多的挑選若干人。A和B兩人必須去一人。A和D不能同時去。A,E,F三人必須兩人去。B和C都
//去或都不去。C和D兩人中去一人。若D不去,E也不去。問應叫哪幾個人去?(靈活運用1與0)
99 static void Mainj(string[] args)
100 {
101 for (int a = 0; a <= 1; a++)
102 {
103 for (int b = 0; b <= 1; b++)
104 {
105 for (int c = 0; c <= 1; c++)
106 {
107 for (int d = 0; d <= 1; d++)
108 {
109 for (int e = 0; e <= 1; e++)
110 {
111 for (int f = 0; f <= 1; f++)
112 {
113 if ((a + b >= 1) && (a + d <= 1) && (a + e + f == 2) && (b + c != 1) && (c + d == 1) && (d - e >= 0))
114 {
115 Console.WriteLine("A=" + a + "B=" + b + "C=" + c + "D=" + d + "E=" + e + "F=" + f);
116 }
117
118 }
119 }
120 }
121 }
122 }
123 }
124 }
125 //老師版
126 static void Mainj1(string[] args)
127 {
128 int a, b, c, d, e, f;
129 for (a = 0; a < 2; a++)
130 {
131 for (b = 0; b < 2; b++)
132 {
133 for (c = 0; c < 2; c++)
134 {
135 for (d = 0; d < 2; d++)
136 {
137 for (e = 0; e < 2; e++)
138 {
139 for (f = 0; f < 2; f++)
140 {
141 if ((a + b >= 1) && (a + d <= 1) && (a + e + f == 2) && (b + c != 1) && (c + d == 1) && ((d + e == 0) || d == 1))
142 {
143 Console.WriteLine("A=" + a + "B=" + b + "C=" + c + "D=" + d + "E=" + e + "F=" + f);
144 }
145 }
146 }
147 }
148 }
149 }
150 }
151 Console.ReadKey();
152 }
b.迭代法:有一定規律。 每次循環都是從上次運算結果中獲得數據,本次運算的結果都是要為下次運算做准備。
eg1 兔生兔問題
有一對幼兔,幼兔一個月後成長為小兔,小兔一個月後成長為成兔並生下一對幼兔,問幾年後有多少對兔子,其中幼兔,小兔,成兔分別是多少?
1 //eg.2 兔生兔問題
2
3 //方法一
4 static void Maink3(string[] args)
5 {
6 int syt = 1, byt = 0;
7 int sxt = 0, bxt = 0;
8 int sct = 0, bct = 0;
9
10 Console.WriteLine("請輸入月數:");
11 int month = Convert.ToInt32(Console.ReadLine());
12 int sum;
13
14 for (int i = 1; i <= month; i++)
15 {
16 //賦值順序不能變,必須按照兔子生長規律來,先有的bct才會有byt
17 bct = sxt + sct;
18 bxt = syt;
19 byt = sxt + sct;
20
21 //bct = sxt + sct; 這樣寫,必須注意他的順序
22 //bxt = syt;
23 //byt = bct;
24
25
26
27 //byt = bct;//錯誤的
28 //bxt = syt;
29 //bct = sxt + sct;
30
31 syt = byt;
32 sxt = bxt;
33 sct = bct;
34
35 //sum = byt + bxt + bct;
36 }
37
38 sum = byt + bxt + bct;
39
40 Console.WriteLine("過了{0}個月後,幼兔個數為{1}對,小兔個數為{2}對,成兔個數為{3}對,總共有{4}對。", month.ToString(), byt, bxt, bct,sum);
41
42 }
43 //方法二
44 static void Maink4(string[] args)
45 {
46 int n = Convert.ToInt32(Console.ReadLine());
47 int tu = 0;//要求那個月的總數
48 int tu1=1, tu2=1;//倒數第一個為 tu1,倒數第二個為 tu2
49
50 for (int i = 3; i < n;i++ )
51 {
52 tu = tu1 + tu2;
53 tu2 = tu1;
54 tu1 = tu;
55 }
56 Console.WriteLine(tu);
57
58 }
59 //方法三
60 static void Maink5(string[] args)
61 {
62 Console.Write("請輸入月數:");
63 int m = int.Parse(Console.ReadLine());
64 int ct = 0;//成兔的對數
65 int xt = 0;//小兔的對數
66 int yt = 1;//
67 int zt = 1;//
68
69 for (int i = 1; i <= m; i++)
70 {
71 if (i == 1)
72 {
73 ct = 0;
74 xt = 0;
75 yt = 1;
76 }
77 else
78 {
79 ct = xt + ct;
80 xt = yt;
81 yt = ct;
82 }
83 zt = yt + xt + ct;
84
85 Console.WriteLine(i.ToString() + "個月後成兔的對數是:" + ct.ToString());
86 Console.WriteLine(i.ToString() + "個月後小兔的對數是:" + xt.ToString());
87 Console.WriteLine(i.ToString() + "個月後幼兔的對數是:" + yt.ToString());
88 Console.WriteLine(i.ToString() + "個月後兔子的總對數是:" + zt.ToString());
89 }
90 Console.ReadLine();
91 }
eg 2 100以內的所有數的和。
eg3. 求階乘。
eg4.求年齡。
eg5.折紙。
eg6.棋盤放糧食。
eg7.猴子吃桃子。
1 static void Maink(string[] args)
2 {
3 int sum = 1;
4 for (int i = 0; i < 6; i++)
5 {
6 int t = (sum + 1) * 2;
7 sum = t;
8 }
9 Console.WriteLine("桃子一共有:" + sum + "個。");
10 }
eg8.落球問題。一個球從10米高度落下,每次彈起2/3的高度,問第五次彈起後的高度?
四、while 循環。一般用在一些死循環當中。
五、try catch。保護程序,避免程序出錯時無法運行。
格式:
try//快捷方式:雙擊 tab鍵
{
}
catch (Exception)
{
throw;
}
finally
{
}