我目前正在制作Rock,Paper,Scissors计划。我们需要包含四个函数,getComputerChoice,getUserChoice,displayChoice和determineWinner。我目前只停留在displayChoice上,我想要显示" Weapon"用户使用字符串选择,但它不起作用。非常感谢任何帮助/反馈,谢谢。
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <string>
using namespace std;
void getComputerChoice(int& computerChoice);
int getUserChoice(int userChoice);
void displayChoice(int userChoice, int computerChoice);
int main()
{
int userChoice = 0, computerChoice;
getUserChoice(userChoice);
getComputerChoice(computerChoice);
displayChoice(userChoice, computerChoice);
return 0;
}
int getUserChoice(int userChoice)
{
cout << "Game Menu\n"
<< "-----------\n"
<< "1. Rock\n"
<< "2. Paper\n"
<< "3. Scissors\n"
<< "4. Quit\n"
<< "Enter your choice (1-4):";
cin >> userChoice;
return (userChoice);
}
void getComputerChoice(int& computerChoice)
{
srand(time(NULL));
computerChoice = (rand() % 3) + 1;
}
void displayChoice(int userChoice, int computerChoice)
{
string uChoice;
if (userChoice == 1)
uChoice = "Rock";
else if (userChoice == 2)
uChoice = "Paper";
else if (userChoice == 3)
uChoice = "Scissors";
cout << "You selected :" << uChoice << endl;
cout << "The computer selected :" << computerChoice << endl;
}
答案 0 :(得分:0)
您的问题是范围之一,您只是返回传递函数的值而不是用户提供的值作为输入,您应该为此使用单独的变量。你的get函数不需要参数,getComputerChoice函数也应该是int类型。这些更改应该为您提供正确的输出:
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <string>
using namespace std;
int getComputerChoice();
int getUserChoice();
void displayChoice(int userChoice, int computerChoice);
int main()
{
int userChoice = 5;
int computerChoice= 5;
userChoice = getUserChoice();
computerChoice = getComputerChoice();
displayChoice(userChoice, computerChoice);
return 0;
}
int getUserChoice()
{
int selection=5;
cout << "Game Menu\n"
<< "-----------\n"
<< "1. Rock\n"
<< "2. Paper\n"
<< "3. Scissors\n"
<< "4. Quit\n"
<< "Enter your choice (1-4):";
cin >> selection;
return selection;
}
int getComputerChoice()
{
srand(time(NULL));
return (rand() % 3) + 1;
}
void displayChoice(int userChoice, int computerChoice)
{
string uChoice;
if (userChoice == 1)
uChoice = "Rock";
else if (userChoice == 2)
uChoice = "Paper";
else if (userChoice == 3)
uChoice = "Scissors";
cout << "You selected : " << uChoice << endl;
cout << "The computer selected : " << computerChoice << endl;
}