我正在製作一個簡單的彩票遊戲應用程序,其中生成0到10之間的三個隨機數字,如果用戶以正確的順序獲得全部三個,他們將得到100萬。如果他們得到一個權利,那麼他們贏得10美元,如果他們得到全部三個,但不是爲了他們贏得一千,如果兩個匹配,然後他們得到1000美元。如果他們沒有得到任何權利,那麼他們什麼也得不到這個C++代碼彩票猜謎遊戲有什麼問題?
這裏是我的代碼在這裏。
INT主要(){
cout << "Hello, this is the lottery! Three random numbers between 0 and 10 will be generated. Guess what they are and the order!" << endl;
char answer;
cout << "Do you want to play? (y or n): " << endl;
cin >> answer;
while (answer == 'y' || 'Y')
{
srand((unsigned)time(NULL));
int ran1 = rand() % 10;
int ran2 = rand() % 10;
int ran3 = rand() % 10;
int guess1, guess2, guess3;
cout << "Enter your first number guess: " << endl;
cin >> guess1;
cout << "Enter your second number guess: " << endl;
cin >> guess2;
cout << "Enter your third number guess: " << endl;
cin >> guess3;
if ((guess1 != ran1 || ran2 || ran3) && (guess2 != ran1 || ran2 || ran3) && (guess3 != ran1 || ran2 || ran3))
cout << "You won no money. Sucks for you." << endl;
else
if ((guess1 == ran1 || ran2 || ran3) || (guess2 == ran1 || ran2 || ran3) || (guess3 == ran1 || ran2) || ran3)
cout << "You won 10 dollars!" << endl;
else
if ((guess1 && guess2 == ran1 && ran2) || (guess1 && guess3 == ran1 && ran3) || (guess2 && guess3 == ran2 && ran3))
cout << "You won 100 dollars!" << endl;
else
if ((guess1 == ran1 || ran2 || ran3) && (guess2 == ran1 || ran2 || ran3) && (guess3 == ran1 || ran2 || ran3))
cout << "You won 1 thousand dollars! good job!" << endl;
else
if ((guess1 == ran1) && (guess2 == ran2) && (guess3 == ran3))
cout << "You won 1 million dollars! jackpot!" << endl;
cout << "The numbers were " << ran1 << "," << ran2 << "," << ran3 << endl;
cout << "Play again?(y or n): " << endl;
cin >> answer;
if (answer == 'y')
continue;
else
break;
}
cout << "Game Over" << endl;
system("pause");
return 0;
}
當我運行這段代碼,事情不正確就決定去了。它所說的是「你沒有贏錢,爲你吸引」。 Idk有什麼不對,也許它有些簡單,但有人可以幫忙嗎?謝謝。
'guess1!= ran1 || ran2 || ran3'不會做你認爲的事 – vu1p3n0x
它有什麼問題? –
看到Sadovsky的答案在下面,你也應該改變你的'if'的順序,即使你猜對了,你也會贏10美元,因爲它滿足條件(和之前的條件) – vu1p3n0x