2012-02-28 66 views
0

当我尝试输入退出字符串xxx时,为什么会抛出异常?它工作在循环,但不与做...而循环。C# - 循环将不接受Convert.ToInt32(...)后的非数字输入

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

这是我的代码。

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

哦,下一次你遇到一些抛出异常的代码时,请告诉哪种异常:),这将有助于整个死亡! :b – f2lollpll 2012-02-28 05:19:11

回答

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

非常感谢。这个逻辑非常好,现在我可以编写** do ... while像我想要的循环。 – kylell 2012-02-28 05:31:18

0

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

我会做的是:

获得右后inValue作出这样的判断是否XXX,就像在你的一段时间(if语句),如果为true,则突破;循环

1

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

你的错误是由这一行造成的:第一= Convert.ToInt32(inValue);

你需要先检查你没有转换字母与数字(因为这将导致异常)。您可以尝试Int32.TryParse()或其他选择。

+0

我查过了。感谢您的提示。 Rajesh Rolen解决了这个问题。 – kylell 2012-02-28 05:33:56