编写一个程序来对剪刀石头布游戏进行评分。两种用户分别以P,R或S键入。程序随后宣布获胜者以及确定获胜者的基础:纸张覆盖岩石,碎剪刀,剪刀剪纸或没人获胜。确保允许用户使用小写字母和大写字母。

//System Libraries
#include <iostream> //Input/Output Library
#include <iomanip>
#include <string>
using namespace std;

//User Libraries

//Global Constants, no Global Variables are allowed
//Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc...

//Function Prototypes

//Execution Begins Here!
int main(int argc, char** argv) {
    //Set the random number seed

    //Declare Variables
    float
           P,p,
           R,r,
           S,s,
           p1,p2;

    //Initialize or input i.e. set variable values
    cout<<"Rock Paper Scissors Game\n";
    cout<<"Input Player 1 and Player 2 Choices\n";
    cin>>p1;
    cin>>p2;
    //Map inputs -> outputs
    if (p1 == p2)
    cout<<"tie";

    if ((p1 == P) && (p2 == R))
     cout<<"Paper covers rock.";
    if ((p1 == p) && (p2 == r))
     cout<<"Paper covers rock.";
    if ((p1 == P) && (p2 == r))
     cout<<"Paper covers rock.";
    if ((p1 == p) && (p2 == R))
     cout<<"Paper covers rock.";

    if ((p1 == S) && (p2 == R))
        cout<<"Rock breaks scissors.";
    if ((p1 == s) && (p2 == r))
        cout<<"Rock breaks scissors.";
    if ((p1 == S) && (p2 == r))
        cout<<"Rock breaks scissors.";
    if ((p1 == s) && (p2 == R))
        cout<<"Rock breaks scissors.";

    if ((p1 == S) && (p2 == P))
        cout<<"Scissors cut paper.";
    if ((p1 == s) && (p2 == p))
        cout<<"Scissors cut paper.";
    if ((p1 == S) && (p2 == p))
        cout<<"Scissors cut paper.";
    if ((p1 == s) && (p2 == P))
        cout<<"Scissors cut paper.";

    //Display the outputs

    //Exit stage right or left!
    return 0;
}

预期:
Rock Paper Scissors Game
Input Player 1 and Player 2 Choices
Paper covers rock

我的结果:
Rock Paper Scissors Game
Input Player 1 and Player 2 Choices
rs
tiePaper covers rock.Paper covers rock.Paper covers rock.Paper covers rock.Rock breaks scissors.Rock breaks scissors.Rock breaks scissors.Rock breaks scissors.Scissors cut paper.Scissors cut paper.Scissors cut paper.Scissors cut paper.

最佳答案

  • 您可以使用tolower而不是比较字母大小写
  • 并且还使用char而不是float
  • 您只需要声明用户输入变量
  • 如果
  • ,则使用else

    代码:
    #include <iostream> //Input/Output Library
    #include <iomanip>
    #include <string>
    using namespace std;
    
    //User Libraries
    
    //Global Constants, no Global Variables are allowed
    //Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc...
    
    //Function Prototypes
    
    //Execution Begins Here!
    int main(int argc, char** argv) {
        //Set the random number seed
    
        //Declare Variables
        char p1,p2;
    
        //Initialize or input i.e. set variable values
        cout<<"Rock Paper Scissors Game\n";
        cout<<"Input Player 1 Choice\n";
        cin>>p1;
        cout<<"Input Player 2 Choice\n";
        cin>>p2;
        //Map inputs -> outputs
        if (p1 == p2)
            cout<<"tie";
        else if ((tolower(p1) == 'p') && (tolower(p2) == 'r'))
            cout<<"Paper covers rock.";
        else if ((tolower(p2) == 'p') && (tolower(p1) == 'r'))
            cout<<"Paper covers rock.";
    
        //or combine above 2 statements to
        //else if (((tolower(p1) == 'p') && (tolower(p2) == 'r')) || ((tolower(p2) == 'p') && (tolower(p1) == 'r')))
            //cout<<"Paper covers rock.";
    
        else if ((tolower(p1) == 's') && (tolower(p2) == 'r'))
            cout<<"Rock breaks scissors.";
        else if ((tolower(p2) == 's') && (tolower(p1) == 'r'))
            cout<<"Rock breaks scissors.";
        else if ((tolower(p1) == 's') && (tolower(p2) == 'p'))
            cout<<"Scissors cut paper.";
        else if ((tolower(p2) == 's') && (tolower(p1) == 'p'))
            cout<<"Scissors cut paper.";
    
        //Display the outputs
    
        //Exit stage right or left!
        return 0;
    }
    

    关于c++ - 我得到的是我所有的输出,而不仅仅是一个,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54211004/

    10-13 06:50