-1

私が作成したプログラムは、2つの質問に応じて、「着る/着る」ものに関する推奨事項をテキストで生成するように設計されています。外の現在の気温と気象条件は何ですか。

3つの範囲があります:(<= 32)(33-55)(56+)度Fそして気象条件の4つの選択:晴れ、曇り、雨、雪

以下のコードを使用すると、32 F未満の温度を入力した場合にのみ、適切な推奨を生成できます。残りの2つの温度範囲に対して同じコードを実行した場合、凍結一時変数入力と同じように推奨を生成するにはどうすればよいですか?

 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 の後にもう 1 つ右中括弧が必要です。そうしないと、他の条件を確認できません。

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...else..は閉じ括弧を逃しました。範囲をif (temperature <= 32) {チェックする前に閉じている必要があります。範囲は既に処理されているため、構造 likeを使用してテストでき、最後に条件を処理できます。(33 - 55)<32else 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
}
于 2013-03-13T05:04:47.827 に答える
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 が true の場合にのみ内側の if に到達することがわかります。また、外側の if が true の場合、いずれにしても内側の if は決して true にならないため、このコードは実行されません。

于 2013-03-13T05:02:42.797 に答える
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 に答える