低于或等于Else IF Java卡住了

时间:2017-11-05 20:19:40

标签: java if-statement

此程序进入温度并根据温度输出消息。我的程序卡住了tempOutside< = 50消息..当我输入-10时,输出是"时间打开热量"。我不能用&&因为这是一个任务问题,我们没有使用这个概念。

    if (tempOutside > 50)
        System.out.println("Enjoy the weather");
    else if (tempOutside <= 50)
        System.out.println("Time to turn on the heat");
    else if (tempOutside <= 30)
        System.out.println("Check the gas in you car before leaving");
    else if (tempOutside <= -10)
        System.out.println("BUNDLE UP it's \"COLD\" outside ");
    else

  1) if-else must be in numerical order (hi to low)
  2) Default must be coded
  3) Repeat the code, but reverse the order(low to hi) 

2 个答案:

答案 0 :(得分:1)

所有int都是> 50<= 50。前两个条件匹配所有条件。

将第二个条件更改为

> 30

第三个条件

> -10

答案 1 :(得分:0)

如果你能直接提到你的确切问题然后你的实施就会更清楚了,因为看起来这里可能存在对这个问题的误解。 但我想也许这就是这里要问的问题?

if (tempOutside > 50)
    {
        System.out.println("Enjoy the weather");
    }
    else if (tempOutside <= 50)
    {
        if(tempOutside <= 30)
        {
            if(tempOutside <= -10)
            {
                System.out.println("BUNDLE UP it's \"COLD\" outside ");

            }
            else {
            System.out.println("Check the gas in you car before leaving");
            }

        }
        else
        {
            //when the temp is between 30 and 50
            System.out.println("Time to turn on the heat");

        }
    }