我正在使用C ++进行Rock Paper Scissors游戏,为了处理异常,我试图为用户输入不是三个字符“ R”之一的情况创建一个if
语句, 'S'或'P',但我只是不知道如何正确构建它而不会收到错误警报。
我去看了我的教科书,看了我的教授关于异常处理的视频,当然也在StackOverflow上,看我的问题是否已经被发现(但我没有发现)。我也不确定我是否正确使用了throw
。
这是我的代码:
#include <iostream>
#include <cctype>
using namespace std;
int main(int argc, const char * argv[]) {
char Player1;
char Player2;
char playAgain = 'Y';
do {
try {
cout << "Welcome to Rock Paper Scissors!" << endl;
cout << "Player 1, you're up! Enter R, P, or S." << endl;
cin >> Player1;
Player1 = toupper(Player1);
cout << "Now Player 2, make your move! R, P, or S." << endl;
cin >> Player2;
Player2 = toupper(Player2);
if( Player1 != 'R' && Player2 != 'R')
throw 0;
else if(Player1 != 'S' && Player2 !='S')
throw 0;
else if(Player1 != 'P' && Player2 !='P')
throw 0;
if (Player1 == Player2)
cout << "Tie game. Play again!";
else if(Player1 == 'R' && Player2 == 'P')
cout << "Paper covers rock, Player 2 wins!";
else if(Player1 == 'R' && Player2 == 'S')
cout << "Rock breaks scissors, Player 1 wins!";
else if(Player1 == 'P' && Player2 == 'R')
cout << "Paper covers rock, Player 1 wins!";
else if(Player1 == 'P' && Player2 == 'S')
cout << "Scissors cut paper, Player 2 wins!";
else if(Player1 == 'S' && Player2 == 'R')
cout << "Rock breaks scissors, Player 2 wins!";
else if(Player1 == 'S' && Player2 == 'P')
cout << "Scissors cut paper, Player 1 wins!";
}
catch(int errID) {
cout << "Error: " << errID << endl;
}
cout << "Do you want to play again? Y/N ";
cin >> playAgain;
playAgain = toupper(playAgain);
}
while (playAgain == 'Y');
return 0;
}
答案 0 :(得分:0)
您的前三个if
语句是错误的。如果两个玩家都未输入R
,则抛出错误。语句需要改写成这样:
if (Player1 != 'R' && Player1 != 'P' && Player1 != 'S')
throw 0;
if (Player2 != 'R' && Player2 != 'P' && Player2 != 'S')
throw 0;
我建议将输入逻辑包装在一些辅助函数中。而且,您也可以清除其余if
条语句。
尝试一下:
#include <iostream>
#include <cctype>
using namespace std;
char getInput(const char *prompt)
{
char input;
cout << prompt << ": ";
if (!(cin >> input)) throw 1;
return toupper(input);
}
char getChoice(const char *prompt)
{
cout << prompt;
char choice = getInput(" Enter R, P, or S");
if ((choice != 'R') && (choice != 'P') && (choice != 'S')) throw 2;
return choice;
}
int main(int argc, const char * argv[]) {
char Player1;
char Player2;
cout << "Welcome to Rock Paper Scissors!" << endl;
do {
try {
Player1 = getChoice("Player 1, you're up!");
Player2 = getChoice("Now Player 2, make your move!");
if (Player1 == Player2)
cout << "Tie game!" << endl;
else if (Player1 == 'R')
{
if (Player2 == 'P')
cout << "Paper covers rock, Player 2 wins!" << endl;
else
cout << "Rock breaks scissors, Player 1 wins!" << endl;
}
else if (Player1 == 'P')
{
if (Player2 == 'R')
cout << "Paper covers rock, Player 1 wins!" << endl;
else
cout << "Scissors cut paper, Player 2 wins!" << endl;
}
else {
if (Player2 == 'R')
cout << "Rock breaks scissors, Player 2 wins!" << endl;
else
cout << "Scissors cut paper, Player 1 wins!" << endl;
}
}
catch (int errID) {
cout << "Error: " << errID << endl;
}
}
while (getInput("Do you want to play again? Y/N") == 'Y');
return 0;
}
或者,考虑使用switch
语句而不是多个if
语句:
#include <iostream>
#include <cctype>
using namespace std;
#define MAKE_USHORT(ch1, ch2) ((static_cast<unsigned short>(ch1) << 8) | static_cast<unsigned short>(ch2))
const char ROCK = 'R';
const char PAPER = 'P';
const char SCISSOR = 'S';
const unsigned short ROCK_ROCK = MAKE_USHORT(ROCK, ROCK);
const unsigned short ROCK_PAPER = MAKE_USHORT(ROCK, PAPER);
const unsigned short ROCK_SCISSOR = MAKE_USHORT(ROCK, SCISSOR);
const unsigned short PAPER_ROCK = MAKE_USHORT(PAPER, ROCK);
const unsigned short PAPER_PAPER = MAKE_USHORT(PAPER, PAPER);
const unsigned short PAPER_SCISSOR = MAKE_USHORT(PAPER, SCISSOR);
const unsigned short SCISSOR_ROCK = MAKE_USHORT(SCISSOR, ROCK);
const unsigned short SCISSOR_PAPER = MAKE_USHORT(SCISSOR, PAPER);
const unsigned short SCISSOR_SCISSOR = MAKE_USHORT(SCISSOR, SCISSOR);
char getInput(const char *prompt)
{
char input;
cout << prompt << ": ";
if (!(cin >> input)) throw 1;
return toupper(input);
}
char getChoice(const char *prompt)
{
cout << prompt;
char choice = getInput(" Enter R, P, or S");
if ((choice != ROCK) && (choice != PAPER) && (choice != SCISSOR)) throw 2;
return choice;
}
unsigned short getChoices()
{
char Player1 = getChoice("Player 1, you're up!");
char Player2 = getChoice("Now Player 2, make your move!");
return MAKE_USHORT(Player1, Player2);
}
int main(int argc, const char * argv[]) {
cout << "Welcome to Rock Paper Scissors!" << endl;
do {
try {
switch (getChoices())
{
case ROCK_ROCK:
case PAPER_PAPER:
case SCISSOR_SCISSOR:
cout << "Tie game!" << endl;
break;
case ROCK_PAPER:
cout << "Paper covers rock, Player 2 wins!" << endl;
break;
case ROCK_SCISSOR:
cout << "Rock breaks scissors, Player 1 wins!" << endl;
case PAPER_ROCK:
cout << "Paper covers rock, Player 1 wins!" << endl;
break;
case PAPER_SCISSOR:
cout << "Scissors cut paper, Player 2 wins!" << endl;
break;
case SCISSOR_ROCK:
cout << "Rock breaks scissors, Player 2 wins!" << endl;
break;
case SCISSOR_PAPER:
cout << "Scissors cut paper, Player 1 wins!" << endl;
break;
}
}
catch (int errID) {
cout << "Error: " << errID << endl;
}
}
while (getInput("Do you want to play again? Y/N") == 'Y');
return 0;
}