C++ RSP比赛结果获胜';我没有出现
我正在为我的下一个盘子做一个石头剪刀游戏,我想我缺少了一些东西,我就是不知道是什么。这是我到目前为止所做的,我所需要的是得到谁赢的结果,以“玩家1赢”或“平局”的形式出现。会不会是循环?char初始化似乎也是错误的。请启发我,我很感谢你的回答!这就是节目的内容 编辑:C++ RSP比赛结果获胜';我没有出现,c++,C++,我正在为我的下一个盘子做一个石头剪刀游戏,我想我缺少了一些东西,我就是不知道是什么。这是我到目前为止所做的,我所需要的是得到谁赢的结果,以“玩家1赢”或“平局”的形式出现。会不会是循环?char初始化似乎也是错误的。请启发我,我很感谢你的回答!这就是节目的内容 编辑: #include <iostream> #include <cstdlib> #include <ctime> #include <stdlib.h> #include <W
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <stdlib.h>
#include <Windows.h>
using namespace std;
class Game
{
private: int rndNum, result;
char P1, P2, repeat;
const char R = 'R', S = 'S', P = 'P';
public: void RSPLogic();
};
void Game::RSPLogic()
{
do {
cout << "Input choice of player 1 : ";
cin >> P1;
cout << "Input choice of player 2 : ";
srand(time(0));
rndNum = rand() % 3;
if (rndNum == 0) //Computer rock
{
P2 = 0;
cout << "R\n";
}
else if (rndNum == 1) //Computer scissors
{
P2 = 1;
cout << "P\n";
}
else if (rndNum == 2) // Computer paper
{
P2 = 2;
cout << "S\n";
}
//Player 1 Win
if ((P1 == 'R' && P2 == 1) || (P1 == 'S' && P2 == 2) || (P1 = 'P' && P2 == 0))
cout << endl << "Player 1 Wins!" << endl << endl;
//Player 2 Win
else if ((P1 == 'R' && P2 == 2) || (P1 == 'S' && P2 == 0) || (P1 == 'P' && P2 == 1))
cout << endl << "Player 2 Wins!" << endl << endl;
//Tie
else if ((P1 == 'R' && P2 == 0) || (P1 == 'S' && P2 == 1) || (P1 == 'P' && P2 == 2))
cout << endl << " It's a Tie!" << endl << endl;
cout << endl << "Press [Y/y] to continue." << endl;
cin >> repeat;
repeat = toupper(repeat);
}
while (repeat == 'Y');
}
int main()
{
Game obj;
obj.RSPLogic();
system("pause");
return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
班级游戏
{
私有:int rndNum,结果;
字符P1,P2,重复;
常量字符R='R',S='S',P='P';
public:void RSPLogic();
};
无效游戏::RSPLogic()
{
做{
cout>P1;
cout将您的方法的开头改为此肯定会有帮助
cout << "Input choice of player 1 : ";
cin >> P1;
cout << "Input choice of player 2 : ";
cin >> P2;
应该是
else if ((P1 == 'R' && P2 == 'R') ...
etc.etc请注意“为什么我的代码不起作用”请查看如何解决您的问题。原因是,您的if/else分支都不匹配。表达式中的某个地方存在逻辑错误。请尝试使用调试器查找;)我打算省略cin>>P2,因为它是计算机对手,应该随机化其输入,因此srand().为了澄清,R应该等于0,P=1和2=S,所以我将P2设为整数,因为随机化器的结果是整数。很抱歉,我发现很难确定explain@applj那么问题是,所有测试都使用P2
,但是具有要测试的值的变量是result
。因此,如果((P1='R'&&result==0).
etc.etc.@applj也rndNum=rand()%3+1;
给你一个从1到3的数字,但是你的如果(rndNum==
测试使用数字0到2。你必须更加小心,计算机会完全按照你说的做,而不是你想说的。@applj或者你的代码应该说P2=0;
而不是result=0;
等等。你的代码中缺少一致性。决定要使用的变量并删除它们剩下的。那会有帮助的。我知道了!我刚把'results'改为'P2'并删除了'+1'。非常感谢!
else if ((P1 == 'R' && P2 == 'R') ...