Advertisement
Spocoman

09. Fruit or Vegetable

Aug 25th, 2024
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.27 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class  FruitOrVegetable {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         String product = scanner.nextLine();
  7.  
  8.         switch (product) {
  9.             case "banana":
  10.             case "apple":
  11.             case "kiwi":
  12.             case "cherry":
  13.             case "lemon":
  14.             case "grapes":
  15.                 System.out.println("fruit");
  16.                 break;
  17.             case "tomato":
  18.             case "cucumber":
  19.             case "pepper":
  20.             case "carrot":
  21.                 System.out.println("vegetable");
  22.                 break;
  23.             default:
  24.                 System.out.println("unknown");
  25.                 break;
  26.         }
  27.     }
  28. }
  29.  
  30. ИЛИ:
  31.  
  32. import java.util.Scanner;
  33.  
  34. public class  FruitOrVegetable {
  35.     public static void main(String[] args) {
  36.         Scanner scanner = new Scanner(System.in);
  37.         String product = scanner.nextLine();
  38.  
  39.         String output = switch (product) {
  40.             case "banana", "apple", "kiwi", "cherry", "lemon", "grapes" -> "fruit";
  41.             case "tomato", "cucumber", "pepper", "carrot" -> "vegetable";
  42.             default -> "unknown";
  43.         };
  44.      
  45.       System.out.println(output);
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement