for循環的基本格式
for(表達式1;表達式2;表達式3)
{
循環體;
}
for循環的四要素
表達式1就是變量初始化;表達式2就是循環條件;表達式3是狀態改變
1 static void Main(string[] args) 2 { 3 // 1、讓用戶輸入一個100以內的數 4 //打印1-100之間所有的數 5 6 for ( int numb = 0; numb <100; numb++) 7 { 8 9 numb++; 10 11 Console.WriteLine(numb); 12 13 14 } 15 Console.ReadLine(); 16 }
這就是一個簡單的循環;它的打印結果是把100以內的數列舉出來
里面的紅色部分就是循環四要素;
接下來說一下循環嵌套:循環嵌套就是再一個循環里面再放一個循環,也就是說如果每一個循環都循環10次,那么第一個循環是1的時候,嵌套的循環會循環十次。也就是10*10的效果。
for 循環語句 主要還是邏輯思維的聯系為主,先放練習;
○○○○★
○○○★★
○○★★★
○★★★★ { 這里圓圈代表空格,先把空格打出來,在輸出星號就會成型}
★★★★★
★★★★★
★★★★
★★★
★★
★
★
★★★
★★★★★
★★★★★★★
★★★★★★★★★
★★★★★★★★★
★★★★★★★
★★★★★
★★★
★
“請輸入一個奇數:”
不是奇數,提示輸入有誤
是奇數
★
★★★
★★★★★
★★★★★★★
★★★★★★★★★
★★★★★★★
★★★★★
★★★
★
1 static void Main(string[] args) 2 { 3 for (int i = 1; i <= 5;i++ ) 4 5 { 6 for (int j = 1; j <= i; j++) 7 { 8 9 Console.Write("★"); 10 } 11 Console.WriteLine(); 12 13 14 } 15 Console.ReadLine();
2
1 static void Main(string[] args) 2 { 3 for (int i = 5; i >=1;i-- ) 4 5 { 6 for (int j = 1; j <=i; j++) 7 { 8 Console.Write("★"); 9 } 10 Console.Write("\n"); 11 12 } 13 Console.ReadLine();
3
1 static void Main(string[] args) 2 { 3 for (int i = 1; i <= 5; i++) 4 { 5 for (int j = 1; j <= 5 - i; j++) 6 { 7 Console.Write("○"); 8 } 9 for (int l = 1; l <= i; l++) 10 { 11 Console.Write("★"); 12 } 13 Console.WriteLine(""); 14 } 15 Console.ReadLine();
4
1 static void Main(string[] args) 2 { 3 for (int i = 1; i <= 5; i++) 4 { 5 for (int j = 2; j <= i; j++) 6 { 7 Console.Write(" "); 8 } 9 for (int l = 1; l <= 6 - i; l++) 10 { 11 Console.Write("★"); 12 } 13 Console.WriteLine(""); 14 } 15 16 Console.ReadLine(); 17 }
以上兩種都是先打印空格在打印星星
有兩種程序,一個是用戶輸入數字最長的那一行就在第幾行,一個是用戶輸入數字,最長的哪一行就會是幾顆星星。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace ff 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 Console.Write("請輸入一個奇數:"); 13 int shu = Convert.ToInt32(Console.ReadLine()); 14 15 int s = (shu + 1) / 2; //輸入幾最長的一行出幾顆星 16 17 if (shu % 2 == 0) 18 { 19 Console.Write("請重新輸入!"); 20 } 21 else 22 { 23 for (int i = 1; i <= shu; i++) 24 { 25 for (int j = 1; j <= shu - i; j++) 26 { 27 Console.Write(" "); 28 } 29 for (int l = 1; l <=2*i-1; l++) 30 { 31 Console.Write("★"); 32 } 33 34 35 Console.WriteLine(""); 36 } 37 for (int i = 1; i <= shu-1; i++) 38 { 39 for (int j = 1; j <= i; j++) 40 { 41 Console.Write(" "); 42 } 43 for (int l = 1; l <= shu - i; l++) 44 { 45 Console.Write("★"); 46 } 47 for (int t = 2; t <= shu - i; t++) 48 { 49 Console.Write("★"); 50 } 51 52 53 Console.WriteLine(""); 54 } 55 56 Console.ReadLine(); 57 } 58 } 59 } 60 }
接下來是跳轉語句:
break - 直接跳出循環,執行下一行代碼
continue - 停止當前此次循環,繼續下一個循環
迭代法:
不斷在自身上增加新的功能
比如
在下面的循環語句中,不斷的w增加。
static void Main(string[] args)
{
// 讓用戶輸入一個100以內的數
//打印1-這個數之間所有的數的和
Console.WriteLine("請輸入100以內的數");
int i = Convert.ToInt32(Console .ReadLine ());
int sum=0;
for (int w= 1; w <=i; w++)
{
sum =sum + w ;
}
Console.WriteLine("他們的和是" + sum);
Console.ReadLine();
}
窮舉法:
代表題目:百雞百錢
將所有的可能性全部列舉出來(循環嵌套不要超過3個,運算量很大)
*異常語句: 這個必須用
try ( 就是你認為那個代碼會出錯, 就在它前面用try並用{}給他括起來。 )
{
可能會出錯的代碼語句
如果這里出錯了,那么不會在繼續下面的代碼,而是直接進入catch中處理異常
}
catch (如果try中的代碼出錯了,就會來執行這個catch中的代碼)
{
如果上面出錯了,這里是對這個異常的處理方式;
}
finally//可寫可不寫,因為可以直接寫 Console.Write();把他代替。
{
不管上面有沒有錯,這里都會走,
}