Advertisement
Spocoman

02. English Name of the Last Digit

Oct 16th, 2024
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.73 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class Main {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int n = Integer.parseInt(scanner.nextLine()) % 10;
  7.        
  8.         String result = switch (n) {
  9.             case 1 -> "one";
  10.             case 2 -> "two";
  11.             case 3 -> "three";
  12.             case 4 -> "four";
  13.             case 5 -> "five";
  14.             case 6 -> "six";
  15.             case 7 -> "seven";
  16.             case 8 -> "eight";
  17.             case 9 -> "nine";
  18.             default-> "zero";
  19.         };
  20.    
  21.         System.out.println(result);
  22.     }
  23. }
  24.  
  25. Solutions with ternary operator:
  26.  
  27. import java.util.Scanner;
  28.  
  29. class Main {
  30.     public static void main(String[] args) {
  31.         Scanner scanner = new Scanner(System.in);
  32.         int n = Integer.parseInt(scanner.nextLine()) % 10;
  33.        
  34.         String result =
  35.             n == 1 ? "one":
  36.             n == 2 ? "two":
  37.             n == 3 ? "three":
  38.             n == 4 ? "four":
  39.             n == 5 ? "five":
  40.             n == 6 ? "six":
  41.             n == 7 ? "seven":
  42.             n == 8 ? "eight":
  43.             n == 9 ? "nine" : "zero";
  44.    
  45.         System.out.println(result);
  46.     }
  47. }
  48.  
  49. Solution with array:
  50.  
  51. import java.util.Scanner;
  52.  
  53. class Main {
  54.     public static void main(String[] args) {
  55.         Scanner scanner = new Scanner(System.in);
  56.         int n = Integer.parseInt(scanner.nextLine()) % 10;
  57.        
  58.         String[] numbers = {
  59.             "zero",
  60.             "one",
  61.             "two",
  62.             "three",
  63.             "four",
  64.             "five",
  65.             "six",
  66.             "seven",
  67.             "eight",
  68.             "nine"
  69.         };
  70.    
  71.         System.out.println(numbers[n]);
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement