简体   繁体   English

如何使用try / catch来检查数字是否在1-3的范围内? C#

[英]How to use try/catch to check for whether a number is within a range of 1-3? c#

        /*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;
            }
        }

I need to make sure a numerical input, as a variable, is between 1 and 3 and was wondering how to do it with C# and Try/Catch . 我需要确保数值输入(作为变量)在1到3之间,并且想知道如何使用C#和Try/Catch I tried (int_bvg_type > 0) && (int_bvg_type < 4); 我尝试了(int_bvg_type > 0) && (int_bvg_type < 4); ... but I get an error saying I cannot use it as a statement. ...但是我收到一条错误消息,说我不能将其用作语句。 Could someone please explain how to check to see if a number is between 1 and 3 using Try/Catch . 有人可以解释一下如何使用Try/Catch检查数字是否在1到3之间。

You need a simple if statement.Not try / catch 你需要一个简单的,如果statement.Not try / catch

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

Or if you insist about using try/catch : 或者,如果您坚持使用try/catch

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

And catch your FormatException : 并捕获您的FormatException

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

Try catch is not the recommended approach. 尝试捕获不是推荐的方法。 Try catch is used for Exceptions. 尝试catch用于异常。 It will only work if your program actually raises an exception. 仅当程序实际引发异常时,它才有效。

My recommended approach would be the following: 我推荐的方法如下:

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

If you still want to do it via try catch then this would do the trick. 如果您仍然想通过try catch做到这一点,那么就可以解决问题。 But frankly, it's just stupid code: 但坦率地说,这只是愚蠢的代码:

    /*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;
        }
    }

You don't need a try/catch statement. 您不需要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;
}

You could also look into int.TryParse if someone might enter a non-numeric character. 如果有人输入非数字字符,您也可以查看int.TryParse

Can i recommend the following as good structure: 我可以推荐以下结构吗?

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

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

Your line is an expression, and it needs to be assigned to a variable, or evaluated, like in an if statement. 您的行是一个表达式,需要像if语句一样将其分配给变量或对其求值。

The try catch is not something you will need, as everyone has said, just use an if statement. 正如每个人都说的那样,try catch不是您所需要的,只需使用if语句即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM