这是一个简单的数字猜谜游戏。如果您猜对了数字,则输出“ You win !!!”,但是如果尝试次数(numberofguesses
)超出,则应输出“ You输”,但显示为“ You win !!”。 !”即使我在numberofguesses
循环之后检查了secretnum
,guess
和while
的值。简单地说,我是一个初学者。
#include <iostream>
using namespace std;
int main()
{
int secretnum = 7;
int guess = 0;
int numberofguesses = 3;
while (secretnum != guess && numberofguesses != 0) {
cout << "enter your guess: ";
cin >> guess;
--numberofguesses;
}
if (secretnum = guess && numberofguesses != 0) {
cout << "You win!!!";
}
else
{
cout << "You lose";
}
}
答案 0 :(得分:2)
您将 assignment 运算符=
与 comparison 运算符==
相混淆。
此行:
if (secretnum = guess && numberofguesses != 0)
cout << "You win!!!";
将其更改为:
if (secretnum == guess && numberofguesses != 0) {
cout << "You win!!!";