2013-03-13 74 views
-1

我制作的程序旨在根据两个问题产生关于“穿着/着装”的文字建议。外面目前的温度和天气情况如何?基本Java程序:if语句?

将有3个范围:(< = 32)(33 - 55)(56+)华氏度 和4个选择为天气状况:晴朗,多云,下雨,下雪

使用下面我代码只有当我输入温度低于32°F时才能生成适当的建议。如果我为剩余的两个温度范围执行了相同的代码,我将如何生成推荐,就像冻结温度变量输入一样?

if (temperature <= 32){ 
      if (weatherCondition == 4){ 
       freezingSnowing(); 
      } 
      else if (weatherCondition == 3){ 
       freezingCloudy(); 
      } 
      else if (weatherCondition == 2){ 
       freezingRain(); 
      } 
      else { 
       freezingSunny(); 
     } 

      if ((temperature >= 33) && (temperature <= 60)){ 
      if (weatherCondition == 4){ 
       warmSnowing(); 
      } 
      else if (weatherCondition == 3){ 
       warmCloudy(); 
      } 
      else if (weatherCondition == 2){ 
       warmRain(); 
      } 
      else { 
       warmSunny(); 
     } 

     } 
     } 

//These are the recommendations that I would like to appear// 


public static void freezingSnowing()  
    { 
     JOptionPane.showMessageDialog(null, "It's is snowing! I recommend that you dress very warm" + 
          "and wear a large coat that is preferably water proof."); 
    } 

    // Temp <= 32 and weather condition = 3 // 
    public static void freezingCloudy() 
    { 
     JOptionPane.showMessageDialog(null, "Yikes it's below freezing, but at least it's just cloudy." + 
          " I would suggest that today you dress very warm and bring rain or snow gear just in case."); 
    } 

    // Temp <= 32 and weather condition = 2 // 
    public static void freezingRain() 
    { 
     JOptionPane.showMessageDialog(null, "Be careful freezing temperatures and rain is very dangerous!" + 
          " If however you will be venturing outside remeber to dress warm and be cautious of icy spots."); 
    } 

     // Temp <= 32 and weather condition = 1 // 
    public static void freezingSunny() 
    { 
     JOptionPane.showMessageDialog(null, "Looks may be decieving today. Don't forget to dress warm" + 
          " it looks nice and sunny out but it is still freezing."); 
    } 


    public static void warmSnowing() 
    { 
     JOptionPane.showMessageDialog(null, "It's is snowing, but based on the temperature it could turn to rain any minute! I recommend that you dress very warm" + 
          "and wear a large coat that is preferably water proof."); 
    } 

} 
+0

此代码'如果((温度> = 33)&&(温度<= 60))'不可达...因为条件执行此代码为'如果(温度<= 32)' – 2013-03-13 05:00:45

+0

您错过了'freezingSunny();'语句后面的'}'。不要在没有任何IDE的情况下编写Java代码。 – 2013-03-13 05:12:59

回答

2

您还没有关闭您的括号正确

if (temperature <= 32){ 
      if (weatherCondition == 4){ 
       freezingSnowing(); 
      } 
      else if (weatherCondition == 3){ 
       freezingCloudy(); 
      } 
      else if (weatherCondition == 2){ 
       freezingRain(); 
      } 
      else { 
       freezingSunny(); 
     } 

应该有一个更右括号别人后,只有这样,你就可以检查其他条件

if (temperature <= 32){ 
      if (weatherCondition == 4){ 
       freezingSnowing(); 
      } 
      else if (weatherCondition == 3){ 
       freezingCloudy(); 
      } 
      else if (weatherCondition == 2){ 
       freezingRain(); 
      } 
      else { 
       freezingSunny(); 
     } 
} 

if ((temperature >= 33) && (temperature <= 60)){ 
      if (weatherCondition == 4){ 
       warmSnowing(); 
      } 
      else if (weatherCondition == 3){ 
       warmCloudy(); 
      } 
      else if (weatherCondition == 2){ 
       warmRain(); 
      } 
      else { 
       warmSunny(); 
      }  
} 
+2

+1。接得好。切勿在没有IDE的情况下编写Java(或者至少使用与大括号相匹配的文本编辑器),并始终正确缩进。 – Thilo 2013-03-13 05:01:11

+0

非常感谢你 – user2162984 2013-03-13 05:07:18

+0

如果有帮助,你能接受答案 – Abi 2013-03-13 05:08:58

1

适当地放入括号。这个代码块将被输入的唯一方式是如果temperature <= 32。另一种情况是不可能进入的。

if (temperature <= 32) { 
     if (weatherCondition == 4) { 
      freezingSnowing(); 
     } else if (weatherCondition == 3) { 
      freezingCloudy(); 
     } else if (weatherCondition == 2) { 
      freezingRain(); 
     } else { 
      freezingSunny(); 
     } 
    } else if ((temperature >= 33) && (temperature <= 60)) { 
     if (weatherCondition == 4) { 
      warmSnowing(); 
     } else if (weatherCondition == 3) { 
      warmCloudy(); 
     } else if (weatherCondition == 2) { 
      warmRain(); 
     } else { 
      warmSunny(); 
     } 

    } 

另外值得一提的是,您weatherCondition值是枚举一个很好的候选人,或使用switch声明。

+0

我认为如果你在评估33 <= x = <60' – 2013-03-13 05:05:22

+0

时,我认为'else if'会更好,最终if语句类似地阅读。是的,那应该是别的 - 如果,但是没有任何方法(用那个代码)''温度'可以在'<=32 && (> = 33 && <= 60)'评估。 – Makoto 2013-03-13 05:06:37

+0

我的意思是,如果(温度<= 32){...}否则如果((温度> = 33)&&(温度<= 60)){...}如果第一个块发生,需要评估第二个区块 – 2013-03-13 05:09:31

1

为什么当你删除一些复杂的代码不工作变得更明显的原因:

if (temperature <= 32) 
{ //outer if begins 
    if (true) 
    { 
     freezingSunny(); 
    } 
    if ((temperature >= 33) && (temperature <= 60)) /* notice we are still inside the outer if */ 
    { //inner if begins 
     if (true) 
     { 
      warmSunny(); 
     } 
    } //inner if ends 
} //outer if ends 

现在我们可以看到,如果外,如果是真正的内部,如果只达到了 - 如果外层如果是真的,内层如果永远不会真的,所以这段代码永远不会运行。

+0

现在我将如何包含天气状况和温度的输入。例如,如果用户输入31度和下雪作为天气状况,我希望能够针对该组合提供特别的建议。我也只是继续这种格式,直到我列出每个温度和天气条件? – user2162984 2013-03-13 05:12:23

+0

@ user2162984你可以这样做,或者你可以创建一个HashTable ,为每个温度带和天气添加一个条目,并使用用户输入从HashTable中获取一个值。要将温度值转换为TemperatureBand,您可以编写一个函数,返回给定温度下的相应TemperatureBand(一个值用于冻结,一个值用于温度等) – Patashu 2013-03-13 05:18:26

+0

您好Patashu,我能够让我的代码与一个以前的评论。然而,作为一个正在学习的人,我对可能使用你的技术感兴趣,但我不确定我是否真的能够真正地概念化如何做到这一点。我是这个网站的新手,但是如果你能给我看一个描述这个过程的简短例子吗?我不会担心,我会稍后再Google它:) – user2162984 2013-03-13 05:46:12

0

您可以使用IF-ELSEIF-ELSE类,以避免混乱上}支架

if (temperature <= 32){ 
     if (weatherCondition == 4){ 
      freezingSnowing(); 
     } 
     else if (weatherCondition == 3){ 
      freezingCloudy(); 
     } 
     else if (weatherCondition == 2){ 
      freezingRain(); 
     } 
     else { 
      freezingSunny(); 
     } 
} 
    else if ((temperature >= 33) && (temperature <= 60)){ 
     if (weatherCondition == 4){ 
      warmSnowing(); 
     } 
     else if (weatherCondition == 3){ 
      warmCloudy(); 
     } 
     else if (weatherCondition == 2){ 
      warmRain(); 
     } 
     else { 
      warmSunny(); 
     } 
} 
else 
{ 
// 3rd range 
} 
+1

也许你可以给一些解释,而不是只提供一个代码。 – 2013-03-13 05:03:50

+0

为什么要投票?有什么问题吗? – 2013-03-13 05:09:33

+0

给出一些解释,而不是只提供一个代码 – 2013-03-13 05:10:49

1

if...else..条件错过了右括号。在检查(33 - 55)范围之前应该已经关闭了if (temperature <= 32) {,并且由于<32范围已经处理,我们可以使用else if构造如else if (temperature <= 55)来测试它,并且最后的else可以处理>55条件。

而不是使用这么多if..else if构造用于测试weatherCondition我宁愿switch..case声明。

我将写像

if (temperature <= 32) { 
    switch (weatherCondition) { 
    case 4: 
     freezingSnowing(); 
     break; 
    case 3: 
     freezingSnowing(); 
     break; 
    case 2: 
     freezingSnowing(); 
     break; 
    default: 
     freezingSunny(); 
    } 
} else if (temperature <= 55) { 
    switch (weatherCondition) { 
    case 4: 
     warmSnowing(); 
     break; 
    case 3: 
     warmCloudy(); 
     break; 
    case 2: 
     warmRain(); 
     break; 
    default: 
     warmSunny(); 
    } 
} else { 
    // handle >=56 
}