for循環語句格式:spa
for (int i = 1;/*初始條件*/i <= 100;/*循環條件*/i++/*狀態改變*/) { //循環體,執行代碼;(break;跳出循環體) }
for 窮舉法
用循環把各類可能的狀況都走一遍,而後用if條件把知足要求的結果給篩選出來。code
for 迭代法blog
有必定規律,從初始狀況按照規律不斷求解中間狀況,最終推導出結果。it
1求幾個數的和for循環
Console.WriteLine("請輸入一個數:"); int a = Convert.ToInt32(Console.ReadLine()); int sum = 0; for (int i = 1; i <= a; i++) { sum = sum + i; } Console.WriteLine("您輸入的這個數的累加和爲:" + sum);
2求階乘class
Console.WriteLine("請輸入一個數:"); int a = Convert.ToInt32(Console.ReadLine()); int jc = 1; for (int i = 1; i <= a; i++) { jc = jc * i; } Console.WriteLine("您輸入的這個數的階乘爲:" + jc); int cj = 1; for (int i = 1; i <= 9; i++) { for (int j = 1; j <= 9; j++) { cj = i * j; Console.Write(i + "*" + j + "=" + cj + " "); } Console.WriteLine(); }
3.求100內的偶數循環
//求100之內的全部偶數 for (int i = 0; i <= 100; i++) { if (i % 2 == 0)//%表明求餘 { Console.WriteLine(i); } }
4.求n次籃球彈起的高度im
//藍球彈起的高度 Console.WriteLine("請輸入次數:"); int a = Convert.ToInt32(Console.ReadLine()); double gao = 5; for (int i = 1; i <= a; i++) { if (i > 5) { gao = 0; } else { gao = gao / 2; } } Console.WriteLine("第" + a + "次彈起的高度爲:" + gao.ToString());