Advertisement
Spocoman

10. Weather Forecast - Part 2

Aug 20th, 2024
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class WeatherForecast2 {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         double degrees = Double.parseDouble(scanner.nextLine());
  7.  
  8.         if (degrees >= 5 && degrees < 12) {
  9.             System.out.println("Cold");
  10.         } else if (degrees >= 12 && degrees < 15) {
  11.             System.out.println("Cool");
  12.         } else if (degrees >=15 && degrees <= 20) {
  13.             System.out.println("Mild");
  14.         } else if (degrees > 20 && degrees < 26) {
  15.             System.out.println("Warm");
  16.         } else if (degrees >= 26 && degrees <= 35) {
  17.             System.out.println("Hot");
  18.         } else {
  19.             System.out.println("unknown");
  20.         }
  21.     }
  22. }
  23.  
  24. WITH TERNARY OPERATOR:
  25.  
  26. import java.util.Scanner;
  27.  
  28. public class WeatherForecast2 {
  29.     public static void main(String[] args) {
  30.         Scanner scanner = new Scanner(System.in);
  31.         double degrees = Double.parseDouble(scanner.nextLine());
  32.        
  33.         String weather =
  34.                 degrees >= 5 && degrees < 12 ? "Cold" :
  35.                         degrees >= 12 && degrees < 15 ? "Cool" :
  36.                                 degrees >=15 && degrees <= 20? "Mild":
  37.                                         degrees > 20 && degrees < 26 ? "Warm" :
  38.                                                 degrees >= 26 && degrees <= 35 ? "Hot" : "unknown";
  39.      
  40.         System.out.println(weather);
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement