Advertisement
Spocoman

04. Personal Titles

Aug 24th, 2024
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.08 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class  PersonalTitles {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         double age = Double.parseDouble(scanner.nextLine());
  7.         String gender = scanner.nextLine();
  8.  
  9.         if (gender.equals("m")) {
  10.             if (age < 16) {
  11.                 System.out.println("Master");
  12.             } else {
  13.                 System.out.println("Mr.");
  14.             }
  15.         } else {
  16.             if (age < 16) {
  17.                 System.out.println("Miss");
  18.             } else {
  19.                 System.out.println("Ms.");
  20.             }
  21.         }
  22.     }
  23. }
  24.  
  25. Решение с тернарен оператор:
  26.  
  27. import java.util.Scanner;
  28.  
  29. public class  PersonalTitles {
  30.     public static void main(String[] args) {
  31.         Scanner scanner = new Scanner(System.in);
  32.         double age = Double.parseDouble(scanner.nextLine());
  33.         String gender = scanner.nextLine();
  34.  
  35.         System.out.println(age >= 16 ? (gender.equals("f") ? "Ms." : "Mr.") : (gender.equals("f") ? "Miss" : "Master"));
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement