控制台不断输入数字,输入end的时候输出最大值

来源:互联网 发布:淘宝能发货到台湾吗 编辑:程序博客网 时间:2024/05/19 10:42

找了几个方法都是把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;        }    }}


0 0
原创粉丝点击