我遇到了我的代碼問題。我必須做出一個簡單的選擇遊戲,我需要這樣做,以便如果用戶輸入一個無效的選項,它會暫停故事,直到他們選擇有效的響應。我嘗試在一會兒(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;
}
}
哪裏循環? – Steve
while循環?我刪除了它,但它在if(firstChoice)行之前。 – crin
這只是一個簡單的錯誤,你應該測試firstChoice2,而不是firstChoice。如果你沒有選擇好的變量名,那麼你會添加錯誤。 –