C++ if 语句中的多个条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9214464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple conditions in C++ if statement
提问by user1200066
I am very new to the concept of programming in C++. I am wanting to have a multi condition if statement using the || (or) and the && (and) in one statement. When I ask my college professor about it. She told it was possible and then insulted my limited knowledge on the subject. All examples I have access to show a multi && statement and only one showing the ||. It does not show them being used together. I would like to learn how to get the line working. I will attach the code I have. The problem area is the last bit of coding.
我对 C++ 编程的概念很陌生。我想要一个使用 || 的多条件 if 语句 (or) 和 && (and) 在一个语句中。当我问我的大学教授这件事时。她说这是可能的,然后侮辱了我对这个主题的有限知识。我有权显示多&& 语句的所有示例,只有一个显示||。它没有显示它们一起使用。我想学习如何让线路工作。我将附上我拥有的代码。问题区域是编码的最后一点。
# include <iostream>
# include <cstring>
using namespace std;
main()
{
const int maximumHours = 774;
char customerPackage;
double hoursUsed = 0,
packageA = 9.95,
packageB = 14.95,
packageC = 19.95,
overPackageA = 2.00,
overPackageB = 1.00,
overTime = 0,
amountDue = 0,
excessCharged = 0;
cout << "Please enter the customer's package: ";
cin >> customerPackage;
switch (customerPackage)
{
case 'a' :
cout << "Please enter the number of hours used: ";
cin >> hoursUsed;
break;
case 'A' :
cout << "Please enter the number of hours used: ";
cin >> hoursUsed;
break;
case 'b' :
cout << "Please enter the number of hours used: ";
cin >> hoursUsed;
break;
case 'B' :
cout << "Please enter the number of hours used: ";
cin >> hoursUsed;
break;
case 'c' :
cout << "Please enter the number of hours used: ";
cin >> hoursUsed;
break;
case 'C' :
cout << "Please enter the number of hours used: ";
cin >> hoursUsed;
break;
default: cout << "Error."
<< " Please enter the customer's purchased package: ";
cin >> customerPackage;
}
if ( customerPackage ='a' || customerPackage ='A' && hoursUsed >= 10)
amountDue = packageA;
else
overTime = packageA - hoursUsed;
excessCharged = overTime * overPackageA;
amountDue = packageA + excessCharged;
}
回答by Mark B
Your problem is that &&
has higher precedence than ||
so you need parens. As noted in a comment you also need to use ==
instead of assignment (=
):
你的问题是它的&&
优先级高于||
所以你需要括号。如评论中所述,您还需要使用==
而不是赋值 ( =
):
if ( (customerPackage =='a' || customerPackage =='A') && hoursUsed >= 10)
if ( (customerPackage =='a' || customerPackage =='A') && hoursUsed >= 10)
回答by Jerry Coffin
Others have already helped you with the problem you've noticed. I'll start with a separate problem you apparently haven't noticed (yet):
其他人已经帮助您解决了您注意到的问题。我将从一个您显然还没有注意到(还)的单独问题开始:
else
overTime = packageA - hoursUsed;
excessCharged = overTime * overPackageA;
amountDue = packageA + excessCharged;
If you want all three of those statements controlled by the else
, you need to enclose them in braces to create a compound statement:
如果您希望所有这三个语句都由 控制,则else
需要将它们括在大括号中以创建复合语句:
else {
overTime = packagA - hoursUsed;
excessCharged = overTime * overPackageA;
amountDue = packageA + excessCharged;
}
As it stands right now, your code is really:
就目前而言,您的代码实际上是:
else
overTime = packageA - hoursUsed;
excessCharged = overTime * overPackageA;
amountDue = packageA + excessCharged;
I.e., the computations for excessCharged
and amountDue
are carried out regardlessof whether the condition in the if
statement was true or false.
即,无论语句中的条件是真还是假excessCharged
,amountDue
都执行和的计算。if
I'd also note that your switch
statement doesn't really accomplish much:
我还要指出,您的switch
声明并没有真正起到多大作用:
switch (customerPackage)
{
case 'a' :
cout << "Please enter the number of hours used: ";
cin >> hoursUsed;
break;
case 'A' :
cout << "Please enter the number of hours used: ";
cin >> hoursUsed;
break;
case 'b' :
cout << "Please enter the number of hours used: ";
cin >> hoursUsed;
break;
case 'B' :
cout << "Please enter the number of hours used: ";
cin >> hoursUsed;
break;
case 'c' :
cout << "Please enter the number of hours used: ";
cin >> hoursUsed;
break;
case 'C' :
cout << "Please enter the number of hours used: ";
cin >> hoursUsed;
break;
default: cout << "Error."
<< " Please enter the customer's purchased package: ";
In particular, you take exactly the same action for all the cases (except the default). You can simplify this a bit by using fall-through cases:
特别是,您对所有情况采取完全相同的操作(默认情况除外)。您可以通过使用失败案例来简化这一点:
switch (customerPackage) {
case 'a':
case 'A':
case 'b':
case 'B':
case 'c':
case 'C':
cout << "Please enter the number of hours used: ";
cin >> hoursUsed;
break;
default:
cout << "Error " /* ... */;
}
Alternatively, you might consider something like:
或者,您可以考虑以下内容:
static const char valid[] = "aAbBcC";
if (strchr(valid, userPackage)) {
cout << "Please enter the number of hours used: ";
cin >> hoursUsed;
}
else {
std::cout << "Error: Please enter the customer's purchased package";
std::cin >> userPackage;
}
Personally, however, I'd structure things a bit differently: first get one valid input, then get the next:
然而,就我个人而言,我的结构有点不同:首先获得一个有效输入,然后获得下一个:
do {
std::cout << "Please enter the customer's purchased package (a, b, or c): ";
std::cin >> userPackage;
} while (!strchr(valid, userPackage));
std::cout << "Please enter the number of hours used: ";
std::cin >> hoursUsed;
if (tolower(customerPackage == 'a') && hoursUsed >= 10)
// ...
回答by Rob?
if ( customerPackage ='a' || customerPackage ='A' && hoursUsed >= 10)
You are soclose to having the right answer. Let me give you two hints:
你是如此接近有正确的答案。让我给你两个提示:
The
=
operator is not the same as the==
operator.=
is the assignment operator. It evaluates its right-hand-side and stores the result in the variable named on its left-hand-side. You want==
, the equality operator. It tests to see if its right-hand side and its left-hand-side are equal.Use parenthesis
( ... )
to enforce your order-of-evaluation intention. You clearly mean to say "If either customerPackage is 'a' or it is 'A', and also hoursUsed is sufficiently large, then ...".
该
=
操作是不一样的==
运营商。=
是赋值运算符。它评估其右侧并将结果存储在其左侧命名的变量中。你想要==
,相等运算符。它测试看它的右手边和它的左手边是否相等。使用括号
( ... )
来强制执行您的评估顺序意图。您显然是想说“如果 customerPackage 是 'a' 或它是 'A',并且 hoursUsed 足够大,那么......”。
Try this line:
试试这一行:
if ( (customerPackage == 'a' || customerPackage == 'A') && hoursUsed >= 10)
回答by Adam Liss
You can use parentheses to specify the order in which the boolean operators are executed. You probably want to evaluate the ||
first, so you'd use:
您可以使用括号来指定布尔运算符的执行顺序。您可能想评估第||
一个,因此您可以使用:
if ((customerPackage == 'a' || customerPackage == 'A') && hoursUsed >= 10)
The &&
is normally evaluated first by default, because it has higher precedence, so your code is equivalent to this:
在&&
通常由默认的第一个评价,因为它具有更高的优先级,让你的代码是相同的:
if (customerPackage == 'a' || (customerPackage == 'A' && hoursUsed >= 10))
Also, as noted in the comments, use ==
for comparison and =
for assignment.
此外,如评论中所述,==
用于比较和=
分配。
回答by Mr Lister
With the new problem you're having (in the other questionyou asked), you'll need some restructuring.
对于您遇到的新问题(在您提出的另一个问题中),您需要进行一些重组。
if ( (customerPackage == 'b' || customerPackage == 'B') && hoursUsed <= 20)
amountDue = packageB;
else
{
/* calculations */
}
is not correct, that should be
不正确,应该是
if ( customerPackage == 'b' || customerPackage == 'B')
{
if (hoursUsed <= 20)
{
amountDue = packageB;
}
else
{
/* calculations */
}
}
Otherwise the first statement will only be executed when package=B AND hour=20, otherwise the calculations will be done in all other cases, like when package is A, or C.
否则第一条语句只会在 package=B AND hour=20 时执行,否则将在所有其他情况下进行计算,例如 package 是 A 或 C 时。
Hope this helps!
希望这可以帮助!