我正在尝试制作一个要求用户输入的程序,然后如果用户选择他们对他们的选择不满意,则可以选择重新启动。如果选择选项然后选择“是”,则程序运行正常,但如果选择“否”,再次选择属性,然后选择“是”,系统会提示用户选择是否再次接受。该计划如下:
有你的属性点!
你接受这些吗?键入1表示是,键入2表示否。如果否,则可以再次选择:
1
你接受这些吗?键入1表示是,键入2表示否。如果否,则可以再次选择:
1
按任意键继续......
以下是代码:
while (true)
{
Console.Write("Do you accept these? Type 1 for Yes, Type 2 for No. If No you can choose again: ");
areYouHappyWithChoices = Console.ReadLine();
if (!UInt32.TryParse(areYouHappyWithChoices, out validChoices))
Console.WriteLine("Please try again. Enter 1 for Yes and 2 for No");
else if (validChoices > 2 || validChoices < 1)
Console.WriteLine("Please try again. Enter 1 for Yes and 2 for No");
else if (areYouHappyWithChoices == "2")
chooseAttributePoints(); //this method contains the whole routine
else
break;
}
答案 0 :(得分:0)
所以,我测试了你的代码,它对我很有用。您的Console.ReadLine()
方法似乎还有chooseAttributePoints()
,因此您必须在致电chooseAttributePoints()
后两次输入您的选择。请检查以确保这是问题所在。
<强>说明:强>
我还重新编写了一些代码,因此它更具可读性并且冗余更少。
int validChoices;
do
{
Console.WriteLine("do some logic ......"); // replace this line with your chooseAttributePoints() method ...
Console.Write("Do you accept these? Type 1 for Yes, Type 2 for No. If No you can choose again: ");
// read input until user gets it right and enters a valid number ...
var areYouHappyWithChoices = Console.ReadLine();
while (!int.TryParse(areYouHappyWithChoices, out validChoices) || validChoices < 1 || validChoices > 2)
{
Console.WriteLine("Please try again. Enter 1 for Yes and 2 for No");
areYouHappyWithChoices = Console.ReadLine();
}
} while (validChoices != 1);