我正在写一个简单的RPG游戏控制台游戏,我将开发与图形加班等,但现在我有我的问题。 .. while循环。我不会经常使用它们,但我认为这对于这种必要性是有益的。基本上,我有一个开篇故事,然后给用户4个选项,他们可以通过按下“1”,“2”,“3”或“4”来输入。如果用户输入其他内容,则应显示一条消息,并允许用户键入正确的输入。我的代码如下:虽然条件没有得到满足,while ...循环仍然保持循环
int action; // Used to determine what the user wants to do.
cout << "Story goes here\n\n";
cout << "What would you like to do? (1/2/3/4)\n\n";
cout << "1. Do this\n";
cout << "2. Do that\n";
cout << "3. Do this that\n";
cout << "4. Party\n\n";
cin >> action;
do
{
switch (action)
{
case 1:
cout << "1\n";
break;
case 2:
cout << "2\n";
break;
case 3:
cout << "3\n";
break;
case 4:
cout << "4\n";
break;
default:
cout << "I'm sorry, but I'm not sure what you want to do. Please tell me again using the corresponding number. (1/2/3/4)\n\n";
cin >> action;
break;
}
} while ((action != 1) || (action != 2) || (action != 3) || (action != 4));
system("pause");
现在我测试过,如果我输入5或6,它显示默认的消息,让我再试一次,不过,如果我键入的内容发生了1,2,3,或者4进入交换机并继续输出我打印出来的号码。它永远不会结束。我在MS Visual Studio 2013 Express中使用C++。
我还使用了调试器,它表示动作等于2(或任何我按下的数字),并继续运行循环。我不知道为什么会发生这种情况。
您的条件逻辑不正确 - 将所有“||”更改为“&&”。 – 2015-02-09 07:57:35
仔细想想,如果“行动”是“2”,条件将会评估什么。 – juanchopanza 2015-02-09 07:58:58