黑马程序员技术交流社区

标题: 使用了未赋值的局部变量,如果赋个初值,用户输入的值无效,最大值就是那个初值! [打印本页]

作者: 黄珍    时间: 2012-5-19 18:30
标题: 使用了未赋值的局部变量,如果赋个初值,用户输入的值无效,最大值就是那个初值!
while (true)
            {
                Console.WriteLine("输入一个数,输入完毕请以end结束输入,将显示最大值");
                string input = Console.ReadLine();
                int numb;
                try
                {
                    numb = Convert.ToInt32(input);
                }
                catch
                {
                    Console.WriteLine("数字格式不正确");
                }
                int mmax = 0;
                if (input == "end")
                {
                    mmax = Max(mmax, numb);
                    Console.WriteLine("你刚才输入的数字最大为:{0}", mmax);
                    Console.ReadKey();
                    break;
                }
               

            }
作者: 王超洋    时间: 2012-5-19 21:11
我把你代码改了一下,这样就可以了:你可以比较一下, if (input == "end")下面的代码要写到catch里,不然输入end也会显示  数字格式不正确 这句话。      
   int mmax = 0;
            int numb = 0;
            while (true)
            {
                Console.WriteLine("输入一个数,输入完毕请以end结束输入,将显示最大值");
                string input = Console.ReadLine();   
                try
                {
                    numb = Convert.ToInt32(input);
                    if (mmax < numb)
                    {
                        mmax = numb;
                    }
                }
                catch
                {
                    if (input == "end")
                    {
                        Console.WriteLine("你刚才输入的数字最大为:{0}", mmax);
                        Console.ReadKey();
                        break;
                    }
                    Console.WriteLine("数字格式不正确");
                }
            }
            Console.ReadKey();
作者: 王国文    时间: 2012-5-19 22:46
王超洋 发表于 2012-5-19 21:11
我把你代码改了一下,这样就可以了:你可以比较一下, if (input == "end")下面的代码要写到catch里,不然 ...

你的程序不管用户怎么输入都会输出:“数字格式不正确”

作者: 王超洋    时间: 2012-5-20 00:10
王国文 发表于 2012-5-19 22:46
你的程序不管用户怎么输入都会输出:“数字格式不正确”

你是意思是让用户输入,如果不是数字就结束程序?上面我改的代码是 如果用户输入的不是数字就循环提示,让用户输入,直到是数字,然后输入end就得出最大值,我运行没有错误,如果不是数字就结束程序的话就这样:
       int mmax = 0;
            int numb = 0;
            Console.WriteLine("输入一个数,输入完毕请以end结束输入,将显示最大值");  //如果想用户每输一次就提示一次的话就把这句话放进while循环里
            while (true)
            {
                string input = Console.ReadLine();    //取得用户输入
                try
                {
                    numb = Convert.ToInt32(input);  //可以转换就进行下面的比较,不可以跳入catch中
                    if (mmax < numb)
                    {
                        mmax = numb;
                    }
                }
                catch
                {
                    if (input == "end")   //是end就进行if语句里的,然后跳出循环结束
                    {
                        Console.WriteLine("你刚才输入的数字最大为:{0}", mmax);
                        break;
                    }
                    Console.WriteLine("数字格式不正确");   //不是end就执行这句话,然后跳出循环结束
                    break;      //这里加个break提示了数字格式不正确就直接退出了
                }
            }
            Console.ReadKey();
作者: 黄珍    时间: 2012-5-20 07:50
后面的总是警告检测到无法访问的代码:后来发现,这是个死循环,没Break;
int bb = 0;
            do
            {
                Console.WriteLine("请输入一个数");
                string input = Console.ReadLine();
                try
                {
                    int number = Convert.ToInt32(input);
                    bb = Max(bb, number);

                }
                catch
                {
                    if (input == "end")
                    {
                        Console.WriteLine("你输入的最大值为:{0}", bb);
                    }
                    else
                    {
                        Console.WriteLine("非数字格式");
                    }
                }
            }
            while (true);

static int Max(int m,int n){
int max;
if(a>b){
max=a;
}
else{
max=b;
}
}





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2