我有一个问题。除了以下几行,这里的所有内容似乎都可以正常工作:
} while (OneMoreTime != 'y' || OneMoreTime != 'n');
完整代码;
#include <iostream>
using namespace std;
int main()
{
int ARRAY_LENGTH = 5;
int MyArray[ARRAY_LENGTH] = {1, 2, 3, 4, 5};
cout << "Values in the array: " << ARRAY_LENGTH << endl;
for (char OneMoreTime = '\0'; OneMoreTime = 'n'; )
{
int WhichNumber = ARRAY_LENGTH;
do
{
cout << "What numbers from the array do you want to see, counting backwards? ";
cin >> WhichNumber;
} while ((WhichNumber > ARRAY_LENGTH) || (WhichNumber <= 0));
//calculating the correct position in the array (from start)
int Number2Print = ARRAY_LENGTH - WhichNumber;
//printing
cout << "The number is: " << MyArray[Number2Print] << endl;
//continue?
do
{
cout << "One more time? (y/n) ";
cin >> OneMoreTime;
} while (OneMoreTime != 'y' || OneMoreTime != 'n');
}
return 0;
}
我得到的是,第一次成功打印后,它不断询问“再过一次?(y / n)”。如果我只使用一种条件,它将起作用(但那还不够)。
最佳答案
该条件将始终为true,因为OneMoreTime
不能同时等于n
和y
。您可能的意思是使用&&
(和)
while (OneMoreTime != 'y' && OneMoreTime != 'n');