尝试制作一个在用户输入无效内容时不断重复的菜单
Trying to make a menu that keeps repeating if the user enters something invalid
这里完全是初学者,昨天才真正开始学习编程,所以请不要评判!
我正在尝试制作一个程序,允许用户输入视频游戏的名称和分数,然后根据要求显示这些分数。我正在尝试制作菜单。我注意到如果用户在没有输入任何数字的情况下按回车键,程序会崩溃,我想避免这种情况,但我被卡住了。如果我按回车键,它不会崩溃。但是,如果我输入 1 或 2,菜单仍然继续,如果我按下 enter 之后没有输入任何内容,那么它会崩溃吗?我迷路了。
namespace videogaems
{
class Program
{
static void Main(string[] args)
{
menu();
}
static void menu()
{
int option = 0;
Console.WriteLine("Select what you want to do: ");
Console.WriteLine("1- add game");
Console.WriteLine("2- show game rating");
bool tryAgain = true;
while (tryAgain)
{
try
{
while (option != 1 || option != 2)
{
option = Convert.ToInt32(Console.ReadLine());
tryAgain = false;
}
}
catch (FormatException)
{
option = 0;
}
}
}
如果字符串无法转换为整数,Convert.ToInt32(Console.ReadLine())
将抛出异常。相反,您应该使用 int.TryParse
,它接受一个 string
和一个设置为转换值(如果成功)的 out
参数。它 returns 一个 bool
表示它是否成功。
例如下面的代码只要int.TryParse
失败就会循环,成功时userInput
会包含转换后的数字:
int userInput;
while (!int.TryParse(Console.ReadLine(), out userInput))
{
Console.WriteLine("Please enter a whole number");
}
但是,另一种选择是简单地使用 Console.ReadKey()
,其中 returns 一个 ConsoleKeyInfo
对象,表示用户按下的键。然后我们可以只检查键字符的值(并忽略任何无效的键)。
例如:
static void Menu()
{
bool exit = false;
while (!exit)
{
Console.Clear();
Console.WriteLine("Select what you want to do: ");
Console.WriteLine(" 1: Add a game");
Console.WriteLine(" 2: Show game rating");
Console.WriteLine(" 3: Exit");
ConsoleKeyInfo userInput = Console.ReadKey();
Console.WriteLine();
switch (userInput.KeyChar)
{
case '1':
// Code to add a game goes here (call AddGame() method, for example)
Console.Clear();
Console.WriteLine("Add a game was selected");
Console.WriteLine("Press any key to return to menu");
Console.ReadKey();
break;
case '2':
// Code to show a game rating goes here (call ShowRating(), for example)
Console.Clear();
Console.WriteLine("Show game rating was selected");
Console.WriteLine("Press any key to return to menu");
Console.ReadKey();
break;
case '3':
// Exit the menu
exit = true;
break;
}
}
}
这里完全是初学者,昨天才真正开始学习编程,所以请不要评判!
我正在尝试制作一个程序,允许用户输入视频游戏的名称和分数,然后根据要求显示这些分数。我正在尝试制作菜单。我注意到如果用户在没有输入任何数字的情况下按回车键,程序会崩溃,我想避免这种情况,但我被卡住了。如果我按回车键,它不会崩溃。但是,如果我输入 1 或 2,菜单仍然继续,如果我按下 enter 之后没有输入任何内容,那么它会崩溃吗?我迷路了。
namespace videogaems
{
class Program
{
static void Main(string[] args)
{
menu();
}
static void menu()
{
int option = 0;
Console.WriteLine("Select what you want to do: ");
Console.WriteLine("1- add game");
Console.WriteLine("2- show game rating");
bool tryAgain = true;
while (tryAgain)
{
try
{
while (option != 1 || option != 2)
{
option = Convert.ToInt32(Console.ReadLine());
tryAgain = false;
}
}
catch (FormatException)
{
option = 0;
}
}
}
Convert.ToInt32(Console.ReadLine())
将抛出异常。相反,您应该使用 int.TryParse
,它接受一个 string
和一个设置为转换值(如果成功)的 out
参数。它 returns 一个 bool
表示它是否成功。
例如下面的代码只要int.TryParse
失败就会循环,成功时userInput
会包含转换后的数字:
int userInput;
while (!int.TryParse(Console.ReadLine(), out userInput))
{
Console.WriteLine("Please enter a whole number");
}
但是,另一种选择是简单地使用 Console.ReadKey()
,其中 returns 一个 ConsoleKeyInfo
对象,表示用户按下的键。然后我们可以只检查键字符的值(并忽略任何无效的键)。
例如:
static void Menu()
{
bool exit = false;
while (!exit)
{
Console.Clear();
Console.WriteLine("Select what you want to do: ");
Console.WriteLine(" 1: Add a game");
Console.WriteLine(" 2: Show game rating");
Console.WriteLine(" 3: Exit");
ConsoleKeyInfo userInput = Console.ReadKey();
Console.WriteLine();
switch (userInput.KeyChar)
{
case '1':
// Code to add a game goes here (call AddGame() method, for example)
Console.Clear();
Console.WriteLine("Add a game was selected");
Console.WriteLine("Press any key to return to menu");
Console.ReadKey();
break;
case '2':
// Code to show a game rating goes here (call ShowRating(), for example)
Console.Clear();
Console.WriteLine("Show game rating was selected");
Console.WriteLine("Press any key to return to menu");
Console.ReadKey();
break;
case '3':
// Exit the menu
exit = true;
break;
}
}
}