2014-01-19 33 views
0
 /*Propmt user for beverages*/ 
     while (!bool_valid) 
     { 
      bool_valid = true; 
      Console.WriteLine("Plesae Choose a Drink; Press 1 for Coke, Press 2 for Sprite, Press 3 for Dr.Prpper"); 
      try 
      { 
       int_bvg_type = Convert.ToInt32(Console.ReadLine()); 
       (int_bvg_type > 0) && (int_bvg_type < 4); 
      } 
      catch 
      { 
       Console.WriteLine("PLease enter a Number between 1 and 3"); 
       bool_valid = false; 
      } 
     } 

我需要確保一個數值輸入,作爲一個變量,介於1和3之間,並想知道如何使用C#和Try/Catch。我試過(int_bvg_type > 0) && (int_bvg_type < 4); ......但是我收到一個錯誤,說我不能用它作爲聲明。有人可以請解釋如何使用Try/Catch檢查數字是否在1和3之間。如何使用try/catch檢查數字是否在1-3的範圍內? c#

+7

我不認爲你想在這裏嘗試一下,if/else聽起來像是一個更好的主意,try/catch應該只用於處理你的控制之外的特殊情況 –

+0

我可以整合第二行try in if/else,然後在Try/Catch後有if/else? – user3063971

+1

@LukeMcGregor以及OP應該可能捕獲可能拋出的FormatException和OverflowException –

回答

3

你需要一個簡單的,如果statement.Not try/catch

if(!(beverageType > 0 && beverageType < 4)) 
{ 
    Console.WriteLine("Please enter a Number between 1 and 3"); 
} 

或者,如果你堅持關於使用try/catch

if(!(beverageType > 0 && beverageType < 4)) 
{ 
    throw new FormatException("Please enter a Number between 1 and 3"); 
} 

,趕上你的FormatException

catch(FormatException ex) 
{ 
    Console.WriteLine(ex.Message); 
} 
+0

有點兒初學者,但是新的FormatException線究竟做了什麼/它是如何工作的? – user3063971

+0

它創建並拋出一個FormatException。您可以閱讀本文檔以獲取更多詳細信息:http://msdn.microsoft.com/en-us/library/ms173163.aspx –

1

你不需要try/catch語句。

bool validInput = false; 

while (!validInput) 
{ 
    Console.WriteLine("Please choose a Drink; Press 1 for Coke, Press 2 for Sprite, Press 3 for Dr.Prpper"); 

    int_bvg_type = int.Parse(Console.ReadLine()); 

    if ((int_bvg_type > 0) && (int_bvg_type < 4)) 
     validInput = true; 
} 

如果有人可能輸入非數字字符,您還可以查看int.TryParse

2

嘗試捕捉不是推薦的方法。嘗試catch用於例外。它只會在你的程序實際引發異常時起作用。

我推薦的做法是以下幾點:

if(!(int_bvg_type > 0 && int_bvg_type < 4)) 
{ 
    Console.WriteLine("PLease enter a Number between 1 and 3"); 
} 

如果仍想通過嘗試catch做那麼這將做的伎倆。但坦率地說,這只是愚蠢代碼:

/*Propmt user for beverages*/ 
    while (!bool_valid) 
    { 
     bool_valid = true; 
     Console.WriteLine("Plesae Choose a Drink; Press 1 for Coke, Press 2 for Sprite, Press 3 for Dr.Prpper"); 
     try 
     { 
      int_bvg_type = Convert.ToInt32(Console.ReadLine()); 
      if(!(int_bvg_type > 0 && int_bvg_type < 4)){ 
       throw new Exception(); 
      } 
     } 
     catch 
     { 
      Console.WriteLine("PLease enter a Number between 1 and 3"); 
      bool_valid = false; 
     } 
    } 
+0

+1。請在回答時嘗試修正拼寫/樣式錯誤。 –

0

我可以推薦以下爲良好的結構:

while(true){ 
    try{ 
     Console.WriteLine("Please Choose a Drink; Press 1 for Coke, Press 2 for Sprite, Press 3 for Dr.Prpper"); 
     switch(int.Parse(Console.ReadLine())){ 
      case 1: 
       //do coke 
       break; 
      case 2: 
       //do sprite 
       break; 
      case 3: 
       //do dr pepper 
       break; 
      default: 
       //your message is shown if no matching rules were found, not based on a secondry range check 
       Console.WriteLine("Im not sure what to do with that option"); 
       break;  
     } 
    } 
    //be explicit about the exceptions you want to catch 
    catch(FormatException){ 
     Console.WriteLine("What you entered wasnt a number"); 
    } 
} 
+0

+0:雖然有效,但此方法明確暗示混合驗證和處理數據。 –

+0

@AlexeiLevenkov在這種情況下,確切點驗證與數據處理同義,因爲它檢查輸入與處理的有效情況匹配。單一責任負責人說,你不應該在這個邏輯上翻倍,因爲它用於完全相同的目的 –

+0

我不確定我在這裏購買SRP參數 - 驗證和處理輸入似乎對我來說是兩個責任(純屬個人)。也許DRY會更有說服力。再次 - 這是我個人的偏好,如果在處理之前沒有其他要驗證的事情,您的回答就很好。 –

0

(int_bvg_type> 0)& &(int_bvg_type < 4);

你的行是一個表達式,它需要被賦值給一個變量,或者像if語句中那樣被賦值。

嘗試捕獲不是你需要的東西,正如每個人所說的,只是使用if語句。

相關問題