找了几个方法都是把max初值设置为0,然后继续后面的步骤.觉得不妥.自己写了个.没想到代码行数这么多,看了几遍,没什么好改的地方,暂时这么贴着吧
个人觉得输入校验用正则比较方便.
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; } }}
新闻热点
疑难解答