gpt4 book ai didi

c++ - 剪刀石头布游戏无法正常运行

转载 作者:行者123 更新时间:2023-11-30 04:51:43 33 4
gpt4 key购买 nike

我正在上计算机科学的初级类(class),对于我的下一个项目,我应该制作一个剪刀石头布游戏。

游戏还没有完成,因为我仍然需要添加记分和输入建议(哦,我应该更频繁地选择)和循环。

不过,我的问题是,我想测试游戏的运行情况,看看它在这之前是否正常运行。代码编译正确,但一旦我输入我的决定并且计算机输入它的决定,“获胜者”就不正确了。

例如:

玩家选择摇滚

电脑选择剪刀

平局

我想也许我输入了一些 if 语句不正确,因为我选择的一些答案是正确的(关于谁是真正的赢家),而有些则没有。

但是在回到我的代码并(多次)检查它们之后,它们对我来说似乎是正确的...

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()

{ // MAIN bracket OPEN

char YorN;
char player;
int computer;

srand(time(NULL));


cout << "****************************************************************************************" << endl;
cout << "**********************************ROCK PAPER SCISSORS***********************************" << endl;
cout << "****************************************************************************************\n\n"<< endl ;

cout << "---Rules of the game is simple. Choose R for Rock, P for Paper, and S for Scissors---\n";
cout << "---Whoever with the most wins, will be crowned victorious ---\n\n";
cout << "Do you think you can beat me? (Y or N): ";

cin >> YorN;
cout << "\n\n\n";

cout<< "NOTE:::: Whenever you're done playing the game, press ( E ) to Exit the game::::" << endl;


if(YorN=='Y' || YorN == 'y')
{ // if statement OPEN

cout << "\nAlright, lets see what you got! "<< endl;

} // if statement CLOSED

else
{ // else staement OPEN

cout<< "\nWow, I beat you without even trying. \nI am VICTORIOUS!!" << endl;

return(0);
} // else statement CLOSED

cout<< "Okay, here we go...\n";
cout<<".\n.\n.\n.\n.\n.\n.\n..." ;
cout << "Choose ( R ) for Rock, ( P ) for Paper, ( S ) for Scissors: " ;

cin >> player;
cout <<"\n\n";
switch(player)
{ // Switch statement OPEN

case 'R':
cout<< "Player chooses Rock " <<endl;
break;
case 'r':
cout<< "Player chooses Rock " <<endl;
break;
case 'P':
cout<<"Player chooses Paper "<< endl;
break;
case 'p':
cout<<"Player chooses Paper "<< endl;
break;
case 'S':
cout<<"Player chooses Scissors "<< endl;
break;
case 's':
cout<<"Player chooses Scissors "<<endl;
break;
default:
cout<<"That is no a correct input "<< endl;

} // Switch statement CLOSED

computer = rand() % 3 + 1;

switch(computer)
{ // Switch statement computer OPEN

case 1:
cout<< "Computer chooses Rock "<< endl;
break;
case 2:
cout<< "Computer chooses Paper "<< endl;
break;
case 3:
cout<< "Computer chooses Scissors "<< endl;
break;


} // Swithch staement computer CLOSED

if(player=='r' || player=='R' && computer==1)
{ // if statement OPEN

cout<< "***It's a TIE***" << endl;

} // if Statement CLOSED

else if(player=='r' || player=='R' && computer==2)
{ // if else statement1 OPEN
cout<<"***Computer WINS***"<< endl;

} // if else statement1 CLOSED
else if(player=='r' || player=='R' && computer==3)
{ //else if statement2 OPEN

cout<<"***Player WINS***"<< endl;

} //else if statement2 CLOSED
else if(player=='p' || player=='P' && computer==1)
{ //else if statement3 OPEN

cout<<"***Player WINS***"<<endl;

} // else if statement3 CLOSED

else if(player=='p' || player=='P' && computer==2)
{ // else if statement4 OPEN

cout<<"***It's a TIE***"<< endl;

} // else if statement4 CLOSED

else if(player=='p' || player=='P' && computer==3)
{ // else if statement5 OPEN

cout<<"***Computer WINS***"<<endl;

} // else if statement5 CLOSED
else if(player=='s' || player=='S' && computer==1)
{ // else if statement6 OPEN

cout<< "***Computer WINS***" << endl;

} // else if statment6 CLOSED

else if(player=='s' || player=='S' && computer==2)
{ // else if statement7 OPEN

cout<< "***Player WINS***"<< endl;

} // eses if statement7 CLOSED

else if(player=='s' || player=='S' && computer==3)
{ // else if statement8 OPEN

cout<< "***It's a TIE***" << endl;

} // else if statement8 CLOSED

} // MAIN bracket CLOSED

同样,编码不完整。我还要补充一些东西。

另外,很抱歉,我对编码还是个新手。如果我对我的问题不够彻底或者我在代码页上添加了太多内容,请原谅我......我不知道应该包含或排除哪些部分

最佳答案

修复 if 语句的所有括号

例子

来自:

if(player=='r' || player=='R' && computer==1){

收件人:

if((player=='r' || player=='R') && computer==1){ 

关于c++ - 剪刀石头布游戏无法正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54729000/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com