我編寫的以下程序提示用戶輸入一個數字1 - 100.該程序有一個來自隨機數發生器的預選號碼,用戶需要猜測。如果用戶的猜測過高或過低,程序會通知用戶,直到用戶的猜測是正確的。我的程序運行良好,但是當提示輸入一個數字時,我必須在某些情況下輸入數字四次或更多次才能提示我。有人能讓我看看或幫我找到我的錯誤嗎?C++:提示需要多次輸入數據才能繼續
#include <iostream>
#include <ctime>//A user stated that using this piece of code would make a true randomization process.
#include <cstdlib>
using namespace std;
int main()//When inputing a number, sometimes the user has to input it more than once.
{
int x;
int ranNum;
srand(time(0));
ranNum = rand() % 100 + 1;
cout << "Please input your guess for a random number between 1 - 100.\n";
cin >> x;
while (x > ranNum || x < ranNum)
{
{
if (x > ranNum)
cout << "Your input was greater than the system's generated random number. Please try again.\n";
cin >> x;
if (x > 100 || x < 1)
cout << "Input invalid. Please input a number between 1 - 100.\n";
cin >> x;
}
{
if (x < ranNum)
cout << "Your input was less than the system's generated random number. Please try again.\n";
cin >> x;
if (x > 100 || x < 1)
cout << "Input invalid. Please input a number between 1 - 100.\n";
cin >> x;
}
{
if (x == ranNum)
cout << "You guessed right!\n";
}
}
return 0;
}
'while(x> ranNum || x