找了幾個方法都是把max初值設置為0,然后繼續(xù)后面的步驟.覺得不妥.自己寫了個.沒想到代碼行數這么多,看了幾遍,沒什么好改的地方,暫時這么貼著吧
個人覺得輸入校驗用正則比較方便.
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Text.RegularExPRessions;namespace sln20170220{ class Program { static void Main(string[] args) { bool b = true; int max; int a; Console.WriteLine("請輸入數組或者end退出"); string str = Console.ReadLine().Trim(); while (!isNumber(str)) { if (str.ToLower() =="end") { return; } Console.WriteLine("請輸入正確的數字或者輸入end退出"); str = Console.ReadLine().Trim(); } max = Convert.ToInt32(str); while (b) { str = Console.ReadLine().Trim(); if (str.ToLower() == "end") { Console.WriteLine("最大值是{0}",max); b=false; } else { if (isNumber(str)) { a = Convert.ToInt32(str); max = a > max ? a : max; } else { Console.WriteLine("請輸入正確的數字或者輸入end退出"); } } } Console.ReadKey(); } private static bool isNumber(string str) { Regex reg = new Regex("^[0-9]+$"); Match ma = reg.Match(str); return ma.Success ? true : false; } }}
新聞熱點
疑難解答