【发布时间】:2014-09-10 07:59:19
【问题描述】:
我一直在尝试制作一个简单的游戏,其中计算机生成一个随机数,然后您尝试猜测它。它还存储您“尝试”的猜测数量。
但是,当我运行该程序时,它只是打印:“让我们玩个游戏。我会想一个数字 1-100。试着猜一下。”
这是我的代码:
#include <iostream>
int main()
{
using namespace std;
int the_number;
int guess;
int tries;
the_number = rand() % 101 + 1;
cout << "Let's play a game!";
cout << "I will think of a number 1-100. Try to guess it.";
cout << endl;
cin >> guess;
for (tries = 0; tries++;)
{
if (guess == the_number)
{
cout << "You guessed it!";
cout << "And it only took you: " << tries;
}
else if (guess < the_number)
{
cout << "Higher";
tries++;
}
else if (guess > the_number)
{
cout << "Lower";
tries++;
}
else
cout << "That's not even in range!";
return 0;
}
}
我不明白为什么这不起作用,有人可以解释为什么不起作用吗?
【问题讨论】:
-
for (tries = 0; tries++;)?