简体   繁体   English

逻辑与或运算符和一个简单的 C++ 问题

[英]Logical AND OR Operators and a simple C++ problem

I am working on a c++ problem from my workbook and I have had a hard time with the behavior of the logical operators in this problem (and many others I have faced.)我正在处理我的工作簿中的 c++ 问题,并且我很难处理这个问题中的逻辑运算符的行为(以及我遇到的许多其他问题。)

Here is the code:这是代码:

#include <iostream>


using namespace std;



int main()
{
string input1, input2;

cout << "Enter two primary colors to mix. I will tell you which secondary color they make." << endl;
cin >> input1;
cin >> input2;


if ((input1 != "red" && input1 != "blue" && input1 != "yellow")&&(input2 != "red" && input2 != "blue" && input2 != "yellow"))
{
    cout << "Error...";
}

else
{
    if (input1 == "red" && input2 == "blue")
    {
        cout << "the color these make is purple." << endl;
    }
    else if (input1 == "red" && input2 == "yellow")
    {
        cout << "the color these make is orange." << endl;
    }
    else if (input1 == "blue" && input2 == "yellow")
    {
        cout << "the color these make is green." << endl;
    }
    
}





system("pause");
return 0;
}

The code works as it should the way it is written.代码按其编写方式工作。 Well, almost.嗯,差不多。 I need the user input to meet certain conditions.我需要用户输入来满足某些条件。 If the user puts any color in other than red, blue, or yellow, I need the program to display an error message.如果用户使用红色、蓝色或黄色以外的任何颜色,我需要程序显示错误消息。 It will not do so the way it is written.它不会按照它的编写方式这样做。 But with the original way it was written, it would only give me an error message, even if I would put in the requisite color.但是按照原来的写法,它只会给我一个错误信息,即使我会输入必要的颜色。 For example:例如:

if ((input1 != "red" || input1 != "blue" || input1 != "yellow")&&(input2 != "red" || 
input2 != "blue" || input2 != "yellow"))

I tried to reason this out in pseudocode to see if it made sense, and it seems like it does.我试图用伪代码对此进行推理,看看它是否有意义,而且看起来确实如此。 Here is my reasoning: If the input1 is not equal to red, blue, or yellow, and input2 is not equal to red, blue or yellow, return an error code.这是我的推理:如果 input1 不等于 red、blue 或 yellow,并且 input2 不等于 red、blue 或 yellow,则返回错误代码。

I can't seem to figure out what I am doing wrong.我似乎无法弄清楚我做错了什么。 Can someone walk me through this?有人可以帮我完成这个吗?

input1 = "red" || input1 = "blue" input1 = "red" || input1 = "blue" is always true, try to think about an input for which this would return false. input1 = "red" || input1 = "blue"总是为真,试着考虑一个输入,它会返回假。 It would have to be equal both to red and blue , that is impossible.它必须等于redblue ,这是不可能的。

If you want "if the input is not any of the options", you want input1 = "red" && input1 = "blue" .如果你想要“如果输入不是任何选项”,你想要input1 = "red" && input1 = "blue" I think you want the following in your first if :我认为您首先需要以下if

if((input1 != "red" && input1 != "blue" && input1 != "yellow") 
   || (input2 != "red" && input2 != "blue" && input2 != "yellow"))

Meaning, "if input1 is not any of these three options or input2 is not any of the other three options, the input is incorrect."意思是,“如果input1不是这三个选项中的任何一个,或者input2不是其他三个选项中的任何一个,则输入不正确。”

In general, put these subexpressions into temporary variables and learn to use a debugger to debug the code.一般来说,将这些子表达式放入临时变量中,并学习使用调试器来调试代码。

Quimby is steering you in the correct direction with his answer to your specific question. Quimby 正在通过他对您的具体问题的回答引导您朝着正确的方向前进。 I wanted to call out another issue and make a suggestion:我想提出另一个问题并提出建议:

If I type "red" then "blue", your program will print "purple".如果我输入“red”然后输入“blue”,您的程序将打印“purple”。 But if I type "blue" then "red" (opposite order), your program will not print "purple" as I would expect.但是如果我输入“blue”然后输入“red”(相反的顺序),你的程序将不会像我期望的那样打印“purple”。

Similarly, if type "red" then "red", I'd expect your program to print "the color these make is red.".同样,如果输入“red”然后输入“red”,我希望你的程序打印“the color these make is red.”。 Your program would not do that either.你的程序也不会那样做。

There's an easy fix which is to use booleans of colors selected:有一个简单的解决方法是使用选择的 colors 的布尔值:

cin >> input1;
cin >> input2;

bool hasRed = (input1 == "red") || (input2 == "red");
bool hasBlue = (input1 == "blue") || (input2 == "blue");
bool hasYellow = (input1 == "yellow") || (input2 == "yellow");

bool isPurple = hasRed && hasGreen;
bool isOrange = hasRed && hasYellow;
bool isGreen = hasBlue && hasYellow;
bool isRed = hasRed && !hasYellow && !hasBlue;
bool isBlue = hasBlue && !hasYellow && !hasRed;
bool isYellow = hasYellow && !hasRed && !hasBlue;

Then your print statements are similar:那么你的打印语句是相似的:

if (isPurple) {
   cout << "You've got purple" << endl;
}
else if (isOrange) {
   cout << "You've got orange" << endl;
}
etc...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM