bool InUse = true;
while (InUse)
Console.WriteLine("Welcome to the 2017 Wimbledon tournament! \n");
Console.WriteLine("Press 1 for a Default tournament");
Console.WriteLine("Press 2 for Women's single:");
Console.WriteLine("Press 3 for Men's single:");
Console.WriteLine("Press 4 for Women's double:");
Console.WriteLine("Press 5 for Men's double:");
Console.WriteLine("Press 6 for Mix double:");
Console.Write("Make a choice:");
int userValue = Convert.ToInt32(Console.ReadLine());
if (userValue == 1 || userValue == 2 || userValue == 3 || userValue == 4 || userValue == 5 || userValue == 6)
{
如果按下正确或错误的话,我似乎无法编写一个返回false和true值的语句。你们这样做怎么样?我打算使用return true;但我似乎无法实现这一点。
答案 0 :(得分:1)
您当前的代码将无限循环,因为您缺少一对大括号。请尝试以下方法:
bool InUse = true;
while (InUse)
{
// Content of the loop goes here
}
请注意,您当前的代码与以下内容相同
bool InUse = true;
while (InUse)
{
Console.WriteLine("Welcome to the 2017 Wimbledon tournament! \n");
}
Console.WriteLine("Press 1 for a Default tournament");
// The rest of your program
答案 1 :(得分:1)
你的格式错误while
已在@ aochagavia的答案中得到解决,因此我将专注于您的输入代码。我会改变
int userValue = Convert.ToInt32(Console.ReadLine());
到
char userValue = Console.ReadKey().KeyChar;
这有两个好处:
Enter
输入的评估可以是这样的:
inUse = false; // assume valid input and set inUse to false to exit while
switch(userValue)
{
case '1':
// Handle Default tournament
break;
case '2':
// Handle Women's single
break;
... // Handle other valid numbers
default:
// Set inUse to true again to ask again
inUse = true;
break;
}
答案 2 :(得分:0)
而不是:
int userValue = Convert.ToInt32(Console.ReadLine());
你可以使用:
int userValue;
bool wasOK = int.TryParse(Console.ReadLine(), out userValue);
if (!wasOK)
{
continue; // restart enclosing 'while' loop
}
之后,您可以:
switch (userValue)
{
// ...
}