Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine()) % 10;
- String result = switch (n) {
- case 1 -> "one";
- case 2 -> "two";
- case 3 -> "three";
- case 4 -> "four";
- case 5 -> "five";
- case 6 -> "six";
- case 7 -> "seven";
- case 8 -> "eight";
- case 9 -> "nine";
- default-> "zero";
- };
- System.out.println(result);
- }
- }
- Solutions with ternary operator:
- import java.util.Scanner;
- class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine()) % 10;
- String result =
- n == 1 ? "one":
- n == 2 ? "two":
- n == 3 ? "three":
- n == 4 ? "four":
- n == 5 ? "five":
- n == 6 ? "six":
- n == 7 ? "seven":
- n == 8 ? "eight":
- n == 9 ? "nine" : "zero";
- System.out.println(result);
- }
- }
- Solution with array:
- import java.util.Scanner;
- class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine()) % 10;
- String[] numbers = {
- "zero",
- "one",
- "two",
- "three",
- "four",
- "five",
- "six",
- "seven",
- "eight",
- "nine"
- };
- System.out.println(numbers[n]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement