外面正在下好大的雨啊,嘿嘿,我就是懶狗一條,太懶了。
第1關:while循環

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace F1 { class Program { static void Main(string[] args) { int[] array = { 1, 2, 6, 9, 4, 7, 13, 12, 5, 3, 8, 10, 11 }; /********** Begin *********/ int temp = 0; int i = 0; //計數器i while (i < array.Length-1) { int j=0; //計數器j while (j < array.Length - 1 - i) { if (array[j] > array[j + 1]) { //開始交換數值 temp = array[j + 1]; array[j + 1] = array[j]; array[j] = temp; } j++; } i++; } /********** End *********/ int size = 0; while (size < array.Length) { Console.WriteLine(array[size]); size++; } } } }
第2關:do循環

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace F2 { class Program { static void Main(string[] args) { /********** Begin *********/ int a = 1; do { Console.WriteLine(a); a = a + 1; } while (a <= 20); Console.ReadLine(); /********** End *********/ } } }
第3關:for循環

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace F3 { class Program { static void Main(string[] args) { int[] array = { 1, 2, 6, 9, 4, 7, 13, 12, 5, 3, 8, 10, 11 }; int temp = 0; /********** Begin *********/ for (int i = 0; i < array.Length - 1; i++) { for (int j = 0; j < array.Length - 1 - i; j++) { if (array[j] > array[j + 1]) { //開始交換數值 temp = array[j + 1]; array[j + 1] = array[j]; array[j] = temp; } } } /********** End *********/ int size = 0; while (size < array.Length) { //輸出排序后的數組 Console.WriteLine(array[size]); size++; } } } }
第4關:foreach循環

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace F3 { class Program { static void Main(string[] args) { string[] nameList = { "Jackie", "Quentin", "Anna", "Charlize", "Eva", "Audrey", "Aoisola" }; /********** Begin *********/ Console.WriteLine("Now start naming"); foreach (var v in nameList) { //執行遍歷操作 Console.WriteLine(v); } Console.WriteLine("Great. Let's start"); /********** End *********/ } } }
第5關:循環中斷

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace F4 { class Program { static void Main(string[] args) { /********** Begin *********/ for (int i = 1; i <= 9; i++) { for (int j = 1; j <= i; j++) { if (i * j == 56) { Console.WriteLine("we found 56"); break; } } Console.Write("\n"); } /********** End *********/ } } }
本來這是昨天的實訓,最近太懶了,昨天就沒有及時發出來,今天才想起寫。