C ++ if语句中的多个条件

时间:2012-02-09 16:10:18

标签: c++ if-statement

我对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;
}

5 个答案:

答案 0 :(得分:11)

您的问题是&&的优先级高于||,因此您需要parens。如评论中所述,您还需要使用==代替分配(=):

if ( (customerPackage =='a' || customerPackage =='A') && hoursUsed >= 10)

答案 1 :(得分:6)

其他人已经帮助你解决了你注意到的问题。我将从一个你显然没有注意到的单独问题开始:

    else
        overTime = packageA - hoursUsed;
        excessCharged = overTime * overPackageA;
        amountDue = packageA + excessCharged;

如果您希望所有这三个语句都由else控制,则需要将它们括在大括号中以创建复合语句:

else {
    overTime = packagA - hoursUsed;
    excessCharged = overTime * overPackageA;
    amountDue = packageA + excessCharged;
}

现在看来,你的代码真的是:

    else
        overTime = packageA - hoursUsed;
    excessCharged = overTime * overPackageA;
    amountDue = packageA + excessCharged;

excessChargedamountDue的计算是在中执行的,无论 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: ";

特别是,您对所有情况采取完全相同的操作(默认情况除外)。您可以使用直通案例来简化这一点:

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 " /* ... */;
}

或者,您可以考虑以下内容:

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;
}

然而,就个人而言,我的结构有点不同:首先得到一个有效的输入,然后得到下一个:

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)
// ...

答案 2 :(得分:4)

if ( customerPackage ='a' || customerPackage ='A' && hoursUsed >= 10)

所以接近得到正确答案。让我给你两个提示:

  1. =运算符与==运算符不同。 =是赋值运算符。它评估其右侧并将结果存储在左侧命名的变量中。你想要==,相等运算符。它会测试它的右侧和左侧是否相等。

  2. 使用括号( ... )强制执行您的评估意图。你明确的意思是说“如果任何一个customerPackage是'a'或者'A',并且hoursUsed足够大,那么......”。

  3. 试试这一行:

    if ( (customerPackage == 'a' || customerPackage == 'A') && hoursUsed >= 10)
    

答案 3 :(得分:0)

您可以使用括号指定布尔运算符的执行顺序。您可能希望首先评估||,因此您将使用:

if ((customerPackage == 'a' || customerPackage == 'A') && hoursUsed >= 10) 

&&通常在默认情况下首先进行评估,因为它具有更高的优先级,因此您的代码等效于此:

if (customerPackage == 'a' || (customerPackage == 'A' && hoursUsed >= 10))

另外,如评论中所述,使用==进行比较,使用=进行分配。

答案 4 :(得分:0)

您遇到的新问题(在您提出的other question中),您需要进行一些重组。

if ( (customerPackage == 'b' || customerPackage == 'B') && hoursUsed <= 20)

    amountDue = packageB;

    else
    {
        /* calculations */
    }

不正确,应该是

if ( customerPackage == 'b' || customerPackage == 'B')
{
   if (hoursUsed <= 20)
   {
      amountDue = packageB;
   }
   else
   {
        /* calculations */
   }
}

否则第一个语句只会在package = B AND hour = 20时执行,否则计算将在所有其他情况下完成,例如当包是A或C时。

希望这有帮助!