C# 只需输入数字即可使用验证规则
嗨,我有一个程序,允许用户从选项1、2和3中进行选择,这是用户选择。但是,当我输入一个大于3的数字或一个字母而不是一个数字时,程序就会崩溃或退出。我想知道是否有一个验证规则,可以放在适当的地方,使数字1至3只能输入C# 只需输入数字即可使用验证规则,c#,C#,嗨,我有一个程序,允许用户从选项1、2和3中进行选择,这是用户选择。但是,当我输入一个大于3的数字或一个字母而不是一个数字时,程序就会崩溃或退出。我想知道是否有一个验证规则,可以放在适当的地方,使数字1至3只能输入 int userChoice; static void Main(string[] args) { new Program().Welcome(); } public void Welcome() {
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)
}
...
您可以使用一个简单的
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。在您的senario中,我建议使用开关盒
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;
}
}
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。在您的senario中,我建议使用开关盒
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);
使用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
}
整数;
if(Int32.TryParse(userChoiceSTR,out编号)&&
number>0&&number可以使用Int32.TryParse方法。它可以确保将像“02”这样的有效答案转换为整数并进行适当处理
int number;
if(Int32.TryParse(userChoiceSTR, out number) &&
number > 0 && number <= 3)
{
Options();
}
else
{
// Handle incorrect user choices
}
整数;
if(Int32.TryParse(userChoiceSTR,out编号)&&
编号>0&&number