【发布时间】:2012-02-09 16:10:17
【问题描述】:
我对 C++ 编程的概念非常陌生。我想在一个语句中使用||(或)和&&(和)有一个多条件if语句。当我问我的大学教授时。她告诉这是可能的,然后侮辱了我在这个问题上的有限知识。我可以访问的所有示例都显示一个多 && 语句,只有一个显示 ||。它没有显示它们一起使用。我想学习如何让生产线正常工作。我将附上我拥有的代码。问题区域是编码的最后一点。
# 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;
}
【问题讨论】:
-
比较
==,而不是=。请务必使用()来消除您的条件的歧义。否则..怎么了?将您的代码称为“问题区域”并没有给我们太多的帮助来确定您希望它做什么而不是做什么。 -
比较运算符在 c/c++ 上是 ==
-
另外,帮自己一个忙,在每个比较前后加上括号,这样你和其他人就可以确定 && 和 || 的顺序。以上。
标签: c++ if-statement