【发布时间】:2015-10-03 07:49:42
【问题描述】:
我的代码有问题。我必须制作一个简单的选择游戏,并且我需要拥有它,这样如果用户输入了一个无效的选项,它就会暂停故事,直到他们选择了一个有效的响应。我试着在一段时间内包装整个东西(1==1)并在他的控制台窗口中输入无效的响应,它会无限打印出“这不是一个选项”。我该如何补救?谢谢。
// First choice, scene 1 if-statements
while (1 == 1)
{
String firstScene = Console.ReadLine();
firstScene = firstScene.Trim();
String firstChoice = firstScene.ToLower();
Console.WriteLine();
if (firstChoice == "rudely")
{
assholeFactor++;
Console.WriteLine(">>\"You're damn right it was! We've been working on this for years. I thought you'd be happy that all those hours at the office actually amounted to something.\" (Douche factor increased)");
break;
}
if (firstChoice == "nicely")
{
niceFactor++;
Console.WriteLine(">>\"No, it wasn't silly. I can see where you're coming from. I suspect there will be a lot of people with those same type of questions upon the release of the first model.\" (Nice factor increased)");
break;
}
if (firstChoice == "silence")
{
judgement--;
Console.WriteLine(">>You sip your wine and say nothing. (Judgement decreased)");
break;
}
if (firstChoice != "rudely" || firstChoice != "nicely" || firstChoice != "silence")
{
Console.WriteLine("That wasn't an option.");
continue;
}
}
【问题讨论】:
-
while 循环?我删除了它,但它在 if(firstChoice) 行之前。
-
这只是一个简单的错误,你应该测试 firstChoice2,而不是 firstChoice。如果您不选择好的变量名称,那么您将添加错误。
-
我认为 C# 允许在字符串上使用
switch? -
确实如此。我喜欢 if 语句:}
标签: c# loops while-loop