#region 輸入數字判斷是否是數字 #region 方法實作 /*不管是實參或形參,都在型別中開辟了空間的; 方法的功能一定要單一; 如GetMax(int n1,int n2); 方法中最忌諱的就是提示用戶輸入的字眼, ///PS:最新版的沒有namespace這些,目前我想到的呼叫方法和類就是自己寫上 */ #endregion /*代碼實作 namespace _方法實作 { class Group { public static void Main(string[] args) { Console.WriteLine("請輸入一個數字:"); string num = Console.ReadLine(); int res = GetNum(num); Console.WriteLine(res); } /// <summary> /// 這個方法需要判斷用戶的輸入是否是數字 /// 如果是數字,則回傳; /// 如果不是數字,則提示用戶重新輸入; /// </summary> /// <param name="num"></param> public static int GetNum(string num) { while (true) { try { int number = Convert.ToInt32(num); return number; } catch { Console.WriteLine("請重新輸入"); num= Console.ReadLine(); } } } } } */ #endregion #region 輸入y或n,輸入y結束,輸入n繼續請求輸入 /* // 輸入y或n,輸入y結束,輸入n繼續請求輸入 namespace _yn { class Group { public static void Main(string[] args) { Console.WriteLine("你是個天才嗎?"); string s=Console.ReadLine(); YesOrNot(s); } /// <summary> /// 輸入y或n,輸入y結束,輸入n繼續請求輸入 /// </summary> /// <param name="s"></param> public static void YesOrNot(string s) { bool b = true; while (b) { if (s == "y") { Console.WriteLine("你真是個天才!"); b = false; } else if(s == "n") { Console.WriteLine("你是個天才嗎?"); s= Console.ReadLine(); } else { Console.WriteLine("你在說什么鬼話,你是天才嗎?"); s= Console.ReadLine(); } } } } } */ #endregion #region 查找兩個整數中的最大值 /* namespace _max { class Group { public static void Main(string[] args) { int n1 = 10; int n2 = 20; int res=GetIntMax(n1,n2); Console.WriteLine(res); } /// <summary> /// 查找兩個整數中的最大值 /// </summary> /// <param name="n1"></param> /// <param name="n2"></param> /// <returns></returns> public static int GetIntMax(int n1,int n2) { int n=(n1 > n2)? n1: n2; return n; } } } */ #endregion #region 計算輸入陣列的和 namespace _sum { class Group { public static void Main(string[] args) { int[] values = { 1, 2, 3, 4, 6, 8, 9, 5, 8, 10 }; int sum=IntSum(values); Console.WriteLine(sum); } /// <summary> /// 計算輸入陣列的和 /// </summary> /// <param name="values"></param> /// <returns></returns> public static int IntSum(int[] values) { int sum = 0; for(int i = 0; i < values.Length; i++) { sum+=values[i]; } return sum; } } } #endregion
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/490136.html
標籤:.NET技术
上一篇:C#學習18————方法呼叫
