只需要输入数字的验证规则

本文关键字:验证 规则 数字 输入 | 更新日期: 2023-09-27 18:24:05

嗨,我有一个程序,允许用户从选项1、2和3中进行选择,这就是userChoice。然而,当我输入一个大于3的数字或一个字母而不是数字时,程序会崩溃或退出。我想知道是否有一个验证规则可以到位,所以只能输入数字1到3。

     int userChoice;
    static void Main(string[] args)
    {
        new Program().Welcome();
    }

    public void Welcome()
    {
        Console.WriteLine("                       HELLO");
        Console.ReadLine();
        Main_Menu();
    }
    private void Main_Menu()
    {
        Console.WriteLine("1). Welcome");
        Console.WriteLine("2). Help Facilities");
        Console.WriteLine("3). Exit");
        string userChoiceSTR = Console.ReadLine();
        if (!string.IsNullOrEmpty(userChoiceSTR))
        {
            userChoice = Convert.ToInt16(userChoiceSTR); 
            try
            {
                Options();
            }
            catch
            {
                Console.WriteLine("Did not put any value. Please Select a menu: ");
                Main_Menu();
            }
        }
        else
        {
            Console.WriteLine("Did not put any value. Please Select a menu: ");
            Main_Menu();
        }
    }
    private void Options()
    {
        if (userChoice == 1)
        {
            Console.Clear();
            Console.WriteLine("Welcome.....................");
            Console.ReadLine();

        }
        if (userChoice == 2)
        {
            Console.Clear();
            Console.WriteLine("Help.........................");
            Console.ReadLine();
        }
        if (userChoice == 3)
        {
        }

只需要输入数字的验证规则

您可以使用这样一个简单的if语句:

...
string userChoiceSTR = Console.ReadLine();
if(userChoiceSTR != "1" && userChoiceSTR != "2" && userChoiceSTR != "3")
{
    Console.WriteLine("You need to provide a value between 1 and 3");
    //Handle the case of invalid input (e.g. return)
}
...

1)您正在检查用户是否输入了一些值。但您并没有检查它是否为整数。你需要确保他输入了整数。将Convert语句移动到try块内。

            try
            {
                userChoice = Convert.ToInt16(userChoiceSTR); 
                Options();
            }
            catch
            {
                Console.WriteLine("Did not put valid value. Please Select a menu: ");
                Main_Menu();
            }

现在,您需要确保他输入的值在1到3之间。在你的情况下,我会建议使用开关的情况。

private void Options()
{
    switch(userChoice)
    {
        case 1:
            Console.Clear();
            Console.WriteLine("Welcome.....................");
            Console.ReadLine();
            break;
        case 2:
            Console.Clear();
            Console.WriteLine("Help.........................");
            Console.ReadLine();
            break;
        case 3:
            break;
        default:
            //Code to handle other values.
            break;
    }

}

使用TryParse而不是Convert方法来验证数字输入。如果用户输入不是数字,则不会引发异常。相反,它将返回一个布尔值false。例如,

//Declare the boolean variable:
bool input = false;
string string1 = "";
//Get the user input and attempt to parse to an int:
string1 = Console.Readline();
input = int.TryParse(string1, out num1);
可以使用Int32.TryParse方法。它将确保像"02"这样的有效答案被转换为整数并得到适当的处理。
int number;
if(Int32.TryParse(userChoiceSTR, out number) &&
    number > 0 && number <= 3)
{
    Options();
}
else
{
    // Handle incorrect user choices  
}

https://msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx