为什么无论输入如何都打印所有if-else语句?

时间:2018-10-03 22:53:19

标签: c++ if-statement

输入任何字母(F,R或G)时,每个if语句都会在编译器中打印。 我不确定为什么会这样,但是一些答案会很好!

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
int tmp;
float precip;
char frg;

cout << "Fall 2018 Automated \"Bruin\" Golf Course Sprinkler System" << endl;
cout << endl << "What is the temperature in degrees(F)? ";
cin >> tmp;
cout << "How much precipitation today (in inches)? ";
cin >> precip;
cout << "The Golf Course grass divisions are F-Fairways, R-Rough, G-Greens.";
cout << endl << "Which do you choose (FRG)? ";
cin >> frg;

if (frg == 'R' && precip < 0.835 && tmp > 38)
    {
        cout << endl << "Given the temperature is " << tmp << " degrees and " << precip << " inches of precipitation today." << endl;
        cout << "The Rough on the Golf Course will be watered.";
    } else
        {
            cout << endl << "Given the temperature is " << tmp << " degrees and " << precip << " inches of precipitation today." << endl;
            cout << "The Rough on the Golf Course will NOT be watered.";
        }

if (frg == 'F' && precip < 0.525 && tmp > 38)
    {
        cout << endl << "Given the temperature is " << tmp << " degrees and " << precip << " inches of precipitation today." << endl;
        cout << "The Fairways on the Golf Course will be watered.";
    } else
        {
            cout << endl << "Given the temperature is " << tmp << " degrees and " << precip << " inches of precipitation today." << endl;
            cout << "The Fairways on the Golf Course will NOT be watered.";
        }

if (frg == 'G' && precip < 0.325 && tmp > 38)
    {
        cout << endl << "Given the temperature is " << tmp << " degrees and " << precip << " inches of precipitation today." << endl;
        cout << "The Greens on the Golf Course will be watered.";
    } else
        {
            cout << endl << "Given the temperature is " << tmp << " degrees and " << precip << " inches of precipitation today." << endl;
            cout << "The Greens on the Golf Course will NOT be watered.";
        }
return 0;
}

即使当我输入R要求输入frg变量时,所有if语句也会在编译器中打印。请帮忙!

谢谢。

2 个答案:

答案 0 :(得分:2)

  

所有if语句都已打印

这不是正在发生的事情。由于if将失败,因此仅打印else语句中的一个,但也打印其他2 if条语句(来自其他两个if)。

我评论了您的代码。

if (frg == 'R' && precip < 0.835 && tmp > 38) {
    // ... your code
} else {
    // Execution will reach this block when frg != R || precip > 0.835 || tmp < 38
    // So if you typed F or G, this else will be executed
}

if (frg == 'F' && precip < 0.525 && tmp > 38) {
    // ... your code
} else {
    // Execution will reach this block when frg != F || precip > 0.525 || tmp > 38
    // So if you typed R or G, this else will be executed
}

if (frg == 'G' && precip < 0.325 && tmp > 38) {
    // ... your code
} else {
    // Execution will reach this block when frg != G || precip > 0.325 || tmp < 38
    // So if you typed R or F, this else will be executed
}

关于您应该如何“解决”此问题,我真的无法提出任何建议,因为我不知道所需的行为是什么。

希望这可以解决问题,

干杯。

答案 1 :(得分:0)

您的逻辑似乎正确,它仅应基于输入字符进入if语句之一。您确定它没有击中一个IF,然后击中另外两个ELSES吗?

您可以添加

 return;

在{}括号内的逻辑末尾,这将确保仅执行一个逻辑{}括号...

否则您将需要一个嵌套的if / else语句来确保仅执行1个{}括号。

现在,您正在运行1 if,然后根据字符运行其他2