我得到的是我所有的输出,而不仅仅是一个

时间:2019-01-16 05:39:02

标签: c++

编写一个程序来对剪刀石头布游戏进行评分。两种用户分别输入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.

3 个答案:

答案 0 :(得分:1)

  • 您可以使用tolower代替比较字母大小写
  • 并且还使用char而不是float
  • 您只需要声明用户输入变量
  • 在其他情况下使用

代码:

server {
    listen 20001;
    listen [::]:20001;

    root /var/www/phpbb;
    index index.php index.html index.htm;

    server_name _;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /var/www/phpbb;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        include fastcgi_params;
    }
}

答案 1 :(得分:1)

在您声明的变量中,您仅初始化(用户输入)p1p2;其余P,p,R,r,S,s仍未初始化并具有垃圾值。

程序中稍后将它们与p1p2进行比较。这是undefined behavior。因此,请先对其进行初始化,然后再进行比较操作。


仅作澄清; P,p,R,r,S,s被称为标识符,并且您需要float值来为其分配(因为它们的类型为float)。

您更有可能需要char而不是float

char  p1, p2;
std::cin >>p1 >> p2;

为了比较,您应该这样做

if (std::to_upper(p1) == 'P' && std::to_upper(p2) == 'R')
    ^^^^^^^^^^^^^^       ^^^^   ^^^^^^^^^^^^^^       ^^^^

请注意,std::toupper用于将字符转换为大写字母,这将减少检查次数。

尽管您需要检查p1p2'P'还是'R''S'中的任何一个,如果它们相等。

使用ternary operator,可以编写一个单一的班轮代码,如下所示:

#include <iostream>
#include <cctype>   // std::toupper

int main()
{
    std::cout << "Input Player 1 and Player 2 Choices\n";
    char p1, p2; std::cin >> p1 >> p2;

    p1 = std::toupper(p1); // convert to upper case
    p2 = std::toupper(p2); // convert to upper case

    std::cout << (
        (p1 == p2 && (p1 == 'P' || p1 == 'R' || p1 == 'S')) ? "tie\n" 
        : (p1 == 'P' && p2 == 'R') || (p1 == 'R' && p2 == 'P') ? "Paper covers rock!\n"
        : (p1 == 'S' && p2 == 'R') || (p1 == 'R' && p2 == 'S') ? "Rock breaks scissors!\n"
        : (p1 == 'S' && p2 == 'P') || (p1 == 'P' && p2 == 'S') ? "Scissors cut paper!\n"
        : "Wrong input!\n"
        );

    return 0;
}

答案 2 :(得分:0)

读起来很像类分配,所以我将描述错误而不是简单地给出工作代码。

您的第一个错误是完全使用float:浮点类型(halffloatdouble等)用于(近似)实数。此处合适的选择是char(一种基本的“字符”类型),但是任何整数类型都应该起作用,因为char也是(通常)最小的整数类型。

>

您的第二个错误是尝试使用变量名(正确地是:标识符)作为字符文字

float P; // uninitialized floating point object
'P' // literal character "P"

您的第三个错误(逻辑错误)是使用相同的变量以相同的顺序进行所有比较 :

// edited to correct previous error
if ((p1 == 'P') && (p2 == 'R')) // checks if p1 wins
 cout<<"Paper covers rock.";
if ((p1 == 'p') && (p2 == 'r')) // checks if p1 wins
 cout<<"Paper covers rock.";
if ((p1 == 'P') && (p2 == 'r')) // checks if p1 wins
 cout<<"Paper covers rock.";
if ((p1 == 'p') && (p2 == 'R')) // checks if p1 wins - shouldn't we check for p2 winning somewhere?
 cout<<"Paper covers rock.";

根据分配限制,您的第四个错误(也是逻辑错误)没有显示哪个玩家获胜。如前所述,您的代码当前只能检测到玩家1的胜利或平局。


您有争议的第五个“错误”是在退出前不打印换行符(将\n附加到字符串,或将<< std::endl附加到std::cout)。