简体   繁体   English

Java 条件天气

[英]Java conditionals weather

Conditional assingment that checks for if less than or greater than a number print the how the weather is like.检查是否小于或大于数字的条件判断会打印出天气情况。 The code seems fine but the some of the checks don't pass had the code checked by two other people that know about java than me.代码看起来不错,但是有些检查没有通过,因为另外两个比我更了解 java 的人检查了代码。 We think that there is a bug on the check/test side.我们认为检查/测试方面存在错误。

here are the instructions这是说明

  1. Prints “The weather is extremely hot!”打印“天气非常热!” IF the user types a number greater than or equal to 90如果用户键入大于或等于 90 的数字

  2. Prints “The weather is mildly hot.”打印“天气有点热。” (else) IF the user types a number less than 90 and greater than 70 (else) 如果用户键入一个小于 90 且大于 70 的数字

  3. Prints “The weather is rainy and hot” (else) IF the user types a number less than 90 and greater than 70 and isRaining is true打印“The weather is raining and hot” (else) 如果用户键入小于 90 且大于 70 的数字并且 isRaining 为真

  4. Prints “The weather is neutral.”打印“天气是中性的。” (else) IF the user types a number between 60 and 70 (greater than 60, but less than 70) (else) 如果用户键入一个介于 60 和 70 之间的数字(大于 60,但小于 70)

  5. Prints “The weather is cold!”打印“天气很冷!” (else) IF the user types a number less than 60 (else) 如果用户输入的数字小于 60

  6. Prints “The weather is rainy and cold” (else) IF the user types a number less than 60 and isRaining is true打印“The weather is raining and cold”(否则)如果用户输入的数字小于 60 并且 isRaining 为真

  7. Prints “The weather is below freezing!”打印“天气低于冰点!” IF the user types a number less than 0如果用户键入小于 0 的数字

Below is my code, 7/10 of the checks passed and at one point 8/10 passed下面是我的代码,7/10 的检查通过了,有一次 8/10 通过了

import java.util.Scanner; 

class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int temperature = sc.nextInt();
    boolean isRaining = sc.nextBoolean();
    // Write your code below this line!
    
    if(temperature >= 90){
      System.out.println("The weather is extremely hot!");
    }
    
    else if(temperature < 90 && temperature > 70 && isRaining == false) {
      System.out.println("The weather is mildly hot.");
    }

    else if((temperature < 90 && temperature > 70) && isRaining == true) {
      System.out.println("The weather is rainy and hot.");
    }

    else if(temperature > 60 && temperature < 70) {
      System.out.println("The weather is neutral.");
    }

    else if(temperature <= 0 && temperature <=60 && isRaining == true) {
      System.out.println("The weather is rainy and cold.");
    }

    if(temperature < 0) {
      System.out.println("The weather is below freezing!");
    }
  
  
}
}

I tired doing if(temperature < 0 && isRaining == false) { System.out.println("The weather is below freezing"); }我累了if(temperature < 0 && isRaining == false) { System.out.println("The weather is below freezing"); } if(temperature < 0 && isRaining == false) { System.out.println("The weather is below freezing"); }

I tired adding the isRaining == true or false to other if else statements that did not require it.我厌倦了将isRaining == truefalse添加到其他不需要它的 if else 语句。

I think user16320675 has given you the answer.我想 user16320675 已经给了你答案。 I just wanted to add that often (if you are using a method for example) using an else statement can shorten your conditionals since the statements before have already done some of the work.我只是想补充一点(例如,如果您正在使用一种方法),使用else语句可以缩短您的条件,因为之前的语句已经完成了一些工作。 You already have many of these so why not use them?您已经拥有其中许多,为什么不使用它们呢?

if(temperature >= 90){
  System.out.println("The weather is extremely hot!");
}

else if(temperature >= 70 && isRaining == false) {
  System.out.println("The weather is mildly hot.");
}

else if(temperature >= 70 && isRaining == true) {
  System.out.println("The weather is rainy and hot.");
}
else if(temperature >= 60) {
  System.out.println("The weather is neutral.");
}
else if(temperature >= 0 && isRaining == true) {
  System.out.println("The weather is rainy and cold.");
}
else if(temperature >= 0) {
  System.out.println("The weather is dry and cold.");
}
else {
  System.out.println("The weather is below freezing!");
}

A couple observations.一些观察。

  • Near the end you are checking temperature twice using the wrong conditional for the first test.接近尾声时,您在第一次测试时使用错误的条件检查了两次温度。
 else if(temperature <= 0 && temperature <=60 && isRaining == true) {
      System.out.println("The weather is rainy and cold.");
    }
  • You can combine one of the tests using the conditional operator for isRaining since the temperature range is the same for both tests.您可以使用isRaining的条件运算符组合其中一项测试,因为这两项测试的温度范围相同。

  • Since you are using 90 degrees for high temperature I presume you are using Fahrenheit.由于您使用的是90度高温,因此我认为您使用的是华氏温度。 So you may want to use 32 for below freezing instead of 0 (although less than 0 is certainly below freezing for both scales).因此,您可能希望将32用于冰点以下而不是0 (尽管对于两个尺度来说,小于0肯定是冰点以下)。

Scanner sc = new Scanner(System.in);
int temperature = sc.nextInt();
boolean isRaining = sc.nextBoolean();
// Write your code below this line!

if(temperature >= 90){
  System.out.println("The weather is extremely hot!");
}

else if(temperature < 90 && temperature > 70) {
  System.out.println("The weather is " + (isRaining ? "rainy and hot." : "mildly hot."));
}

else if(temperature > 60 && temperature < 70) {
  System.out.println("The weather is neutral.");
}

else if(temperature >= 32 && temperature <=60 && isRaining) {
  System.out.println("The weather is rainy and cold.");
} 

else if(temperature < 32) {
  System.out.println("The weather is below freezing!");
}
 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM