第八节课第三个作业

2018-11-14  本文已影响0人  流影随风

#编码

//不断要求用户输入一个数字(假定用户输入的都是正整数),当用户输入end的时候显示刚才输入的数字中的最大值

           string str_num = "";

            int max = 0;

            do

            {

                Console.WriteLine("输入数字");

                str_num = Console.ReadLine();

                try

                {

                    if (str_num == "end")

                    {

                        Console.WriteLine("退出");

                    }

                    else

                    {

                        int num = Convert.ToInt32(str_num);

                        if (num > max)

                        {

                            max = num;

                        }

                    }

                }

                catch

                {

                    Console.WriteLine("输入类型错误");

                }

            }         

            while (str_num != "end");

            Console.WriteLine("最大值{0}", max);

            Console.ReadKey();


string strNumber = "";

int max = 0;//用来存储最大值

while (strNumber != "end")

            {

Console.WriteLine("请输入一个数字,输入end,我们将显示你输入的数字中的最大值");

                strNumber =Console.ReadLine();//数字end  乱七八糟

if (strNumber != "end")

                {

try

                    {

int number = Convert.ToInt32(strNumber);

if(number > max)

                        {

                            max = number;

                        }

                    }

catch

                    {

Console.WriteLine("输入有误,请重新输入");

                    }

                }

else//输入的是end

                {

Console.WriteLine("您刚才输入的数字中最大值是{0}",max);

                }

            }

Console.ReadKey();

#效果

上一篇 下一篇

猜你喜欢

热点阅读