2012-02-28 34 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