-1

我制作的程序旨在根据两个问题在文本中生成关于“穿/着装”的建议。目前外面的温度和天气情况如何。

将有 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.");
   }

}
4

5 回答 5

2

您没有正确关闭括号

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

else 之后应该还有一个右大括号,只有这样你才能检查其他条件

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();
          }    
 }
于 2013-03-13T05:00:05.927 回答
1

适当地嵌套你的括号。输入此代码块的唯一方法是 if 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语句的良好候选者。

于 2013-03-13T05:02:08.993 回答
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 无论如何都不会为真,所以这段代码永远不会运行。

于 2013-03-13T05:02:42.797 回答
1

if...else..的条件错过了右括号。在if (temperature <= 32) {检查范围之前应该已经关闭,(33 - 55)并且由于<32范围已经被处理,我们可以使用else if类似的构造来测试它,else if (temperature <= 55)并且 lastelse可以处理>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
}
于 2013-03-13T05:04:47.827 回答
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
}
于 2013-03-13T05:02:56.400 回答