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

I am getting all of my outputs instead of just one

本文关键字:一个 输出      更新时间:2023-10-16

编写一个程序为纸岩剪刀游戏打分。两个用户中的每一个都键入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 Gamen";
cout<<"Input Player 1 and Player 2 Choicesn";
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
  • 您只需要声明用户输入变量
  • 如果

代码:

#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 Gamen";
cout<<"Input Player 1 Choicen";
cin>>p1;
cout<<"Input Player 2 Choicen";
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;
}

在您声明的变量中,您只初始化了(用户输入)p1p2;它们中的其余CCD_ 3仍然未初始化并且具有垃圾值。

在程序的后面,您将把它们与p1p2进行比较。这是一种未定义的行为。因此,在对它们执行比较操作之前,请对它们进行初始化。


仅供澄清;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'中的任何一个,如果它们相等。

使用三元运算符,可以编写如下的单行代码:

#include <iostream>
#include <cctype>   // std::toupper
int main()
{
std::cout << "Input Player 1 and Player 2 Choicesn";
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')) ? "tien" 
: (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;
}

这读起来很像一个类赋值,所以我将描述错误,而不是简单地展示工作代码

第一个错误是使用floats:浮点类型(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)。