我对C++编程的概念很陌生.我希望有一个多条件if语句使用|| (或)和&&(和)在一个陈述中.当我问我的大学教授时.她说这是可能的,然后侮辱了我对这个问题的有限知识.我有权访问的所有示例都显示了一个multi &&语句,只有一个显示了||.它没有显示它们一起使用.我想学习如何使线路正常工作.我会附上我的代码.问题区域是编码的最后一点.
# 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;
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*k B 11
你的问题是&&优先级高于||你需要的parens.如评论中所述,您还需要使用==而不是赋值(=):
if ( (customerPackage =='a' || customerPackage =='A') && hoursUsed >= 10)
其他人已经帮助你解决了你注意到的问题.我将从一个你显然没有注意到的单独问题开始(还):
else
overTime = packageA - hoursUsed;
excessCharged = overTime * overPackageA;
amountDue = packageA + excessCharged;
Run Code Online (Sandbox Code Playgroud)
如果您希望由这些语句控制所有这三个语句else,则需要将它们括在大括号中以创建复合语句:
else {
overTime = packagA - hoursUsed;
excessCharged = overTime * overPackageA;
amountDue = packageA + excessCharged;
}
Run Code Online (Sandbox Code Playgroud)
就目前而言,您的代码确实是:
else
overTime = packageA - hoursUsed;
excessCharged = overTime * overPackageA;
amountDue = packageA + excessCharged;
Run Code Online (Sandbox Code Playgroud)
即,无论声明中的条件是真还是假excessCharged,amountDue都执行和计算.if
我还要注意你的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: ";
Run Code Online (Sandbox Code Playgroud)
特别是,您对所有情况采取完全相同的操作(默认情况除外).您可以通过使用直通案例来简化这一点:
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 " /* ... */;
}
Run Code Online (Sandbox Code Playgroud)
或者,你可以考虑这样的事情:
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;
}
Run Code Online (Sandbox Code Playgroud)
但就个人而言,我的结构有点不同:首先得到一个有效的输入,然后得到下一个:
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)
// ...
Run Code Online (Sandbox Code Playgroud)
if ( customerPackage ='a' || customerPackage ='A' && hoursUsed >= 10)
Run Code Online (Sandbox Code Playgroud)
你已经非常接近得到正确答案了。让我给你两个提示:
操作员=与操作员不一样==。=是赋值运算符。它计算其右侧并将结果存储在其左侧命名的变量中。您需要==,等于运算符。它测试其右侧和左侧是否相等。
使用括号( ... )来强制执行您的评估顺序意图。你的意思显然是说“如果customerPackage是或者'a'它是'A',并且也hoursUsed足够大,那么......”。
试试这一行:
if ( (customerPackage == 'a' || customerPackage == 'A') && hoursUsed >= 10)
Run Code Online (Sandbox Code Playgroud)