我得到了我所有的输出,而不是一个
I am getting all of my outputs instead of just one
编写一个程序为纸岩剪刀游戏打分。两个用户中的每一个都键入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;
}
在您声明的变量中,您只初始化了(用户输入)p1
和p2
;它们中的其余CCD_ 3仍然未初始化并且具有垃圾值。
在程序的后面,您将把它们与p1
和p2
进行比较。这是一种未定义的行为。因此,在对它们执行比较操作之前,请对它们进行初始化。
仅供澄清;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用于将字符转换为大写,这将减少您所做的检查次数。
尽管您需要检查p1
和p2
是否是'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;
}
这读起来很像一个类赋值,所以我将描述错误,而不是简单地展示工作代码
第一个错误是使用float
s:浮点类型(half
、float
、double
等)用于(近似)实数。这里合适的选择是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
)。
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- C++试图读取一个文件并输出到另一个文本文件
- 我写了一个C++程序来模拟Enigma机器.我没有得到输出
- 如何使用C++读取另一个程序中的源代码输出
- 我从int x[3]得到的一个非常奇怪的输出;
- Qt creator 4.11,在应用程序输出面板中创建一个链接
- 我的输出中有一个额外的 0,为什么会这样
- 以C++输出一个数字三角形
- 当使用我在一个函数、另一个函数中修改的数组时,在我的输出中得到一个奇怪的负数
- 有没有办法我可以在 C++ 中使用 if 语句来说明如果一个字符串不是两个名称的 eqal,它会给出一个输出,ATM i
- C++:为什么最后一个输出不是地址以及如何获取值 9
- 如何仅使用循环/控制语句和两个cout语句创建一个倒三角:一个输出“#”和一个输出' n'
- 如何制作一个输出流,将未指定的类型括在C++的括号内
- C++我在运行时创建一个输出文件,如何再次读取此输出文件作为输入
- 我不明白这段代码的最后一个输出
- 将两个正则表达式结果连接到一个输出字段中,一次只能使用一个
- 为什么我得到一个输出"b(1) dc dvfunc"?
- 矩阵从一个输入文件到LAPACK,再反向到一个输出文件
- 从文件流创建一个输出文件