转换后,C# - Loop不接受非数字输入.ToInt32(...)

时间:2012-02-28 05:06:03

标签: c# casting formatting compiler-errors

当我尝试输入退出字符串xxx时,为什么会抛出异常?它适用于 while 循环,但不适用于 do ... while 循环。

转换为整数后,字符串变量只允许我使用数字字符(如-999)退出 do ... while 循环。但我想让循环控制变量像“退出”而不是数字。我怎么能这样做?

这是我的代码。

using System;
namespace StringInputPractice
{
    class StringInputPractice
    {
        static void Main()
        {
            // Declarations

            string inValue;
            int first;
            int second;
            int sum;

            do
            {
                Console.Write("\nEnter the first number. (Type \"xxx\" to exit):   ");
                inValue = Console.ReadLine();

                first = Convert.ToInt32(inValue);   //@@@@@
                Console.Write("\nEnter the second number. (Type \"xxx\" to exit):   ");
                inValue = Console.ReadLine();
                second = int.Parse(inValue);

                sum = first + second;

                Console.WriteLine("\nThe sum of {0} and {1} is {2}.", first,
                    second, sum);

                /* Things I've tried inside do { } and that don't work */

                //inValue = "";
                //inValue = null;
                //inValue = inValue.ToString();
                //inValue = first.ToString();
                //inValue = second.ToString();
            }

            while (inValue != "xxx");   /*If you enter a non-numeric string,
                                         * an exception is thrown at
                                         * @@@@@ above.
                                         */

            Console.ReadLine();
        }
    }
}

3 个答案:

答案 0 :(得分:2)

试试这个:使用int.TryParse代替Convert.ToInt32

public void myfun()
        {
            string inValue;
            int first;
            int second;
            int sum;

            do
            {
                Console.Write("\nEnter the first number. (Type \"xxx\" to exit):   ");
                inValue = Console.ReadLine();

                if (int.TryParse(inValue, out first))
                {
                   // first = Convert.ToInt32(inValue);   //@@@@@
                    Console.Write("\nEnter the second number. (Type \"xxx\" to exit):   ");
                    inValue = Console.ReadLine();
                    if(int.TryParse(inValue, out second))
                    {
                   // second = int.Parse(inValue);

                    sum = first + second;

                    Console.WriteLine("\nThe sum of {0} and {1} is {2}.", first,
                        second, sum);
                    }
                }
                /* Things I've tried inside do { } and that don't work */

                //inValue = "";
                //inValue = null;
                //inValue = inValue.ToString();
                //inValue = first.ToString();
                //inValue = second.ToString();
            }

            while (inValue != "xxx");   /*If you enter a non-numeric string,
                                     * an exception is thrown at
                                     * @@@@@ above.
                                     */

            Console.ReadLine();
        }

答案 1 :(得分:1)

您使用什么程序进行编码?大多数程序允许您调试应用程序。这意味着您可以冻结程序并逐行运行。在每一行上,您可以检查变量的值,如果有异常 - 您将能够看到它。

您的错误是由此行引起的:first = Convert.ToInt32(inValue);

您需要首先检查您是否未将字母转换为数字(因为这会导致异常)。您可以尝试使用Int32.TryParse()或其他替代方法。

答案 2 :(得分:0)

您如何期望将xxx转换为数字?这不可能!

我要做的是:

在获取inValue之后立即创建一个if语句,检查它是否为xxx,就像在while()中一样,如果为true则断开;循环