c# - C#—在Convert.ToInt32(...)之后,循环将不接受非数字输入

标签 c# casting formatting compiler-errors

当我尝试输入退出字符串xxx时,为什么会引发异常?它在循环中以的方式工作,但在循环中不与 do ...一起使用。

转换为整数后,字符串变量仅允许我使用数字字符(如-999)退出 do ...,而循环。但是我想使循环控制变量成为“quit”而不是数字之类的词。我怎样才能做到这一点?

这是我的代码。

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();
        }
    }
}

最佳答案

试试这个:使用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();
        }

关于c# - C#—在Convert.ToInt32(...)之后,循环将不接受非数字输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9476694/

相关文章:

c# - File.AppendAllText 是否在操作后关闭文件

c# - 如何检索邮件项目的 Outlook 文件夹 (Outlook.MailItem)?

Javascript:将 Math.sqrt 转换为 int?

c++ - 这段代码中的类型转换在哪里?

ios - 如何检查字典值是字符串还是数组

c++ - 是否可以在一个函数中使用两次 va_list 方法?

没有零填充的 Python 日期时间格式

c# - 将 Open XML Excel 文件转换为 HTML

c# - linq to sql中如何判断是否使用join?

mysql - 将 MySQL 表重新格式化为网格