我最近開始學習C++,想做我的第一個「遊戲」/程序。我遇到了一些困難。C++ - 我編寫我的第一個程序,但我遇到了一些麻煩
我的錯誤,至今如下:
(rand()%a)
- >改變「一」 doesn't做任何事情(例如,如果生成的數2和「a」是1生成的數字停留2)。下面的代碼不起作用:
while(!(b = c)){ cout << "Enter your guess! \n"; cin >> c; if(c<b){ cout << "Bigger! \n"; } if(c>b){ cout << "Smaller! \n"; } d++; }
我的完整的程序如下:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
// Max. Limit
int a;
// Random Number
int b;
// Guess
int c;
// Tries counter
int d = 0;
cout << "Enter highest possible number, setting the max. limit for the program. \n";
cin >> a;
srand(time(0));
b = 0 + (rand()%a);
if(b =! 1){
c = 1;
}
if(b = 1){
c = 2;
}
while(!(b = c)){
cout << "Enter your guess! \n";
cin >> c;
if(c<b){
cout << "Bigger! \n";
}
if(c>b){
cout << "Smaller! \n";
}
d++;
}
if(b=c){
cout << "Congratulations! You have guessed was right! The number was indeed " << b << " !" << endl;
cout << "You needed " << d << " tries to find the number! \n";
}
return 0;
}
。 – drescherjm
因此,使用b == c而不是b = c – antlersoft
也有'b =! c'應該是'b!= c' – mike510a