2014-01-19 32 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语句。

相关问题