基本Java程序:if语句?

时间:2013-03-13 04:57:08

标签: java

我所制作的计划旨在根据两个问题在文本中提出关于“穿/穿”的建议。外面的当前温度和天气条件是什么。

将有3个范围:(< = 32)(33-55)(56+)°F 天气状况有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.");
   }

}

5 个答案:

答案 0 :(得分: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();
          }    
 }

答案 1 :(得分: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语句的理想选择。

答案 2 :(得分: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

现在我们可以看到只有当外部if为真时才会到达内部if - 如果外部if为真,那么内部if永远不会为真,所以这段代码永远不会运行。

答案 3 :(得分:1)

if...else..条件错过了结束括号。在检查if (temperature <= 32) {范围之前(33 - 55)应该已经关闭,并且由于已经处理了<32范围,我们可以使用else ifelse 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
}

答案 4 :(得分: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
}