問題出在第三行的循環中。我嘗試過嘗試++和++嘗試,但是,當我執行程序時,它返回爲嘗試01。我需要幫助才能獲得正確數量的循環計數。簡單的C++代碼,這給我一個錯誤的循環計數。需要幫助來糾正它
// Guess My Number
// The classic number guessing game
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(static_cast<unsigned int>(time(0))); //seed random number generator
int secretNumber = rand() % 100 + 1; // random number between 1 and 100
int tries = 0;
int guess;
cout << "\tWelcome to Guess My Number\n\n";
do
{
cout << "Enter a guess: ";
cin >> guess;
++tries;
if (guess > secretNumber)
{
cout << "Too high!\n\n";
}
else if (guess < secretNumber)
{
cout << "Too low!\n\n";
}
else
{
cout << "\nThat's it! You got it in " << tries << " guesses!\n";
}
} while (guess != secretNumber);
return 0;
}
但是您在代碼段 – timrau 2014-12-13 06:10:47
中寫了'tries + = tries;'是的。但是,結果仍然是一樣的。每次運行時,循環運行50次都無關緊要,嘗試值返回爲01。 – 2014-12-13 06:13:01
適用於我(使用++試試編輯)。您確定您在更改後成功地重新編譯了嗎? – 2014-12-13 06:16:08