我正在制作一个简单的随机数猜猜游戏(下面的例子),它完美无缺,直到你的猜测高于数字。当它发生时,它打印:"我的数字低于那个数字高于那个"当它只是打印"我的号码低于那个"。此外,当您猜对了正确的答案时,它会打印"我的号码高于"
我的代码是:
#include <iostream>
#include <unistd.h>
#include <cstdlib>
int main() {
int highnum;
int guess;
srand(time(0));
std::cout << "Pick a number greater than 2";
std::cin >> highnum;
int randynumeber = (rand () % highnum) + 1;
std::cout << "I have picked a random number between 1 and " << highnum << " which you need to try and guess";
int guesses = 0;
int stop = 0;
while (guess != randynumeber)
{
if (stop == 0)
{
sleep(0.1);
stop = 1;
std::cout << std::endl << "Guess a number";
std::cin >> guess;
if(guess>randynumeber)
std::cout << "My number is lower than that";
else if(guess<randynumeber)
std::cout << "My number is higher than that";
stop = 0;
}
}
std::cout << std::endl << "guesses = " << guesses;
std::cout << "You win!";
}
答案 0 :(得分:0)
您没有在else语句中测试条件。你需要测试它,否则它只是执行( guess < randynumeber);
- 它给出一个逻辑答案,然后执行下一段代码,即下一个cout
语句。