第1关:写一个函数
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace G1 { class Program { static void Main(string[] args) { string myWords = Hello(); Console.WriteLine(myWords); } /********** Begin *********/ static string Hello() { return "Hello World, this is my function!"; } /********** End *********/ } }
第2关:迭代函数
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace G2 { class Program { static void Main(string[] args) { int[] a = { 3, 4, 33, 2, 121, 34, 65, 34 }; int size = a.Length - 1; int result = Max(a, size); Console.WriteLine(result); } /********** Begin *********/ static int Max(int[] array, int i) { int temp; if (i == 0) { return array[0]; } else { temp = Max(array, i - 1); if (temp > array[i]) { return temp; } else { return array[i]; } } } /********** End *********/ } }
第3关:变量的作用域
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace G3 { class Program { static void Main(string[] args) { outputDiscount(); } /********** Begin *********/ static int originalPrice = 100; static float discount = 0.73F; static void outputDiscount() { Console.WriteLine("After discounting, the price is " + originalPrice * discount); } /********** End *********/ } }
第4关:引用参数和值参数
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace G4 { class Program { static void Main(string[] args) { int totalPrice = 100; ; /********** Begin *********/ int originalPrice = 100; float discount = 0.73F; /********** End *********/ outputDiscount(originalPrice, discount, ref totalPrice); Console.WriteLine("main totalPrice is " + totalPrice); } /********** Begin *********/ static void outputDiscount(int orgPrice, float dis,ref int total) { total = (int)(orgPrice * dis); Console.WriteLine("After discounting, the price is " + total); } /********** End *********/ } }