【问题标题】:Multiple conditions in C++ if statementC++ if 语句中的多个条件
【发布时间】: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


【解决方案1】:

您的问题是&amp;&amp; 的优先级高于||,因此您需要括号。如评论中所述,您还需要使用== 而不是赋值(=):

if ( (customerPackage =='a' || customerPackage =='A') &amp;&amp; hoursUsed &gt;= 10)

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 首先感谢所有帮助我的人。我已根据此处共享的信息进行了更改,并在应有的地方给予信用。我几乎完成了这个程序。我有两个问题。一个是问题是通过所有包裹并计算错误的金额。我已经返回并针对在需要时不脱离 if 语句的问题进行了建议的更改。我知道这是我缺少的一小步。再次感谢大家。
    【解决方案3】:
    if ( customerPackage ='a' || customerPackage ='A' && hoursUsed >= 10)
    

    如此接近于正确答案。让我给你两个提示:

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

    2. 使用括号( ... ) 强制执行您的评估顺序意图。你的意思显然是说“如果customerPackage'a' 或者它是'A',并且hoursUsed 足够大,那么......”。

    试试这条线:

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

    【讨论】:

      【解决方案4】:

      您可以使用括号指定布尔运算符的执行顺序。你可能想先评估||,所以你会使用:

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

      &amp;&amp; 通常默认首先被评估,因为它具有更高的优先级,所以你的代码相当于这样:

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

      此外,如 cmets 中所述,使用 == 进行比较,使用 = 进行分配。

      【讨论】:

      • 谢谢。这是非常有用的信息,如果我需要再次寻求帮助,我会更详细地介绍。
      【解决方案5】:

      对于您遇到的新问题(在您提出的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 时执行,否则将在所有其他情况下进行计算,例如 package 为 A 或 C 时。

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-24
        • 2015-01-20
        • 2013-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多