-2
我是一名初學者,我無法找到我在這個岩石剪刀程序中做錯了什麼。有人可以幫忙嗎?以下是我的代碼:C++中的岩石紙剪刀程序
我創建了兩個函數one userchoice(),它接受來自用戶的輸入並返回相應的字符串。第二個是compchoice(),隨機數從1到3生成,並且返回對應的字符串,然後我調用這個函數並將該值保存在一個while循環中比較的變量中,以查看用戶輸入是否與計算機匹配。
using namespace std;
string compchoice();
string userchoice();
int main() {
//cout<<"computer picks a choice here"<<compchoice()<<endl;
//cout<<"pick your choice "<<userchoice()<<endl;
string comp, user;
comp = compchoice();
user = userchoice();
while (userchoice() != compchoice()) {
if (userchoice() != compchoice()) {
cout << " go again \n\t";
comp = compchoice();
user = userchoice();
} else {
cout << "congratulations\t computer choice was " << comp <<
" user choice is " << user;
//comp = compchoice();
//user = userchoice();
}
}
}
string compchoice() {
srand(time(0));
int ch;
string choiceStr;
ch = rand() % 3 + 1;
switch (ch) {
case 1:
choiceStr = "rock";
break;
case 2:
choiceStr = "paper";
break;
case 3:
choiceStr = "scissors";
break;
default:
cout << "computer menu.... existing";
//break;
}
return choiceStr;
}
string userchoice() {
string choiceStr;
int choice;
cout << " 1. rock \n";
cout << " 2. paper \n";
cout << " 3. scissors \n";
cout << " user menu make selection : \n";
cin >> choice;
cout << endl;
switch (choice) {
case 1:
choiceStr = "rock";
break;
case 2:
choiceStr = "paper";
break;
case 3:
choiceStr = "scissors";
break;
default:
choiceStr = "enter only 1, 2, or 3";
//break;
}
return choiceStr;
}
有無數個'石頭紙scissor'上做題(我猜它必須是一個共同的功課問題)。你看過他們中的任何一個嗎? – Barmar
有什麼問題? – esel
你的'#include'在哪裏?你有什麼問題,輸入?輸出? – zero298