-2
我需要在案例陳述中隨機挑選櫻桃,桔子,羽毛,鈴鐺,瓜子或酒吧,然後以某種方式顯示選擇的內容我可以比較他們,但我不知道如何。我不確定如何分配我想要的值
例如,我希望當我打印slot1,slot2和slot3時,我會得到三個開關中每個開關內的case語句的名稱。
不是他們的號碼。 (所以它現在是相當混亂的程序還沒有完成)
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int main()
{
int slot1;
int slot2;
int slot3;
double won;
double money;
string cherries;
string oranges;
string plums;
string bells;
string melons;
string bars;
string doAgain;
do
{
cout << "We are going to be playing a slot machine game today." << endl;
cout << "Please enter the amount of money you'd like to insert into the slot machine." << endl;
cin >> money;
cout << "You put in $" << money << endl;
srand(time(0));
slot1=rand()%6+1;
slot2=rand()%6+1;
slot3=rand()%6+1;
switch (slot1)
{
case 1:
cout << cherries << endl;
case 2:
cout << oranges << endl;
break;
case 3:
cout << plums << endl;
break;
case 4:
cout << bells << endl;
break;
case 5:
cout << melons << endl;
break;
case 6:
cout << bars << endl;
}
switch (slot2)
{
case 1:
cout << melons << endl;
break;
case 2:
cout << bells << endl;
break;
case 3:
cout << bars << endl;
break;
case 4:
cout << plums << endl;
break;
case 5:
cout << oranges << endl;
break;
case 6:
cout << cherries << endl;
}
switch (slot3)
{
case 1:
cout << bars << endl;
break;
case 2:
cout << plums << endl;
break;
case 3:
cout << melons << endl;
break;
case 4:
cout << bells << endl;
break;
case 5:
cout << oranges << endl;
break;
case 6:
cout << cherries << endl;
}
cout << "The numbers you got were " << slot1 << ", " << slot2 << ", " << slot3 << endl;
cout << "Would you like to play again?" << endl;
cin >> doAgain;
if(doAgain!= "yes")
{
cout << "The total amount of money you put in the slot machine is" << money << endl;
cout << "The total amount of money you won is $" << won << endl;
}
}
while(doAgain=="yes");
return 0;
}
enter code here
你聲明變量'cherries','oranges'等等,但是從來沒有給它們分配任何東西,所以它們都是空字符串。你爲什麼不直接用'cout <<「櫻桃\ n」'''這樣的字符串呢? – Barmar
你可以請你花一點點努力來格式化你的代碼,使其更具可讀性?謝謝。 – user3078414
可讀性是什麼意思? :) – FBHSIE