Advertisement
Spocoman

03. Animal Type

Aug 24th, 2024
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.23 KB | None | 0 0
  1. import java.util.Objects;
  2. import java.util.Scanner;
  3.  
  4. public class  AnimalType {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         String animal = scanner.nextLine();
  8.  
  9.         if (Objects.equals(animal, "dog")) {
  10.             System.out.println("mammal");
  11.         } else if
  12.         (Objects.equals(animal, "crocodile")
  13.                         || Objects.equals(animal, "tortoise")
  14.                         || Objects.equals(animal, "snake")) {
  15.             System.out.println("reptile");
  16.         } else {
  17.             System.out.println("unknown");
  18.         }
  19.     }
  20. }
  21.  
  22. Решение с switch:
  23.  
  24. import java.util.Scanner;
  25.  
  26. public class  AnimalType {
  27.     public static void main(String[] args) {
  28.         Scanner scanner = new Scanner(System.in);
  29.         String animal = scanner.nextLine();
  30.  
  31.         switch (animal) {
  32.             case "dog":
  33.                 System.out.println("mammal");
  34.                 break;
  35.             case "crocodile":
  36.             case "tortoise":
  37.             case "snake":
  38.                 System.out.println("reptile");
  39.                 break;
  40.             default:
  41.                 System.out.println("unknown");
  42.                 break;
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement