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()),
- sms;
- String message = "";
- for (int i = 0; i < n; i++) {
- sms = Integer.parseInt(scanner.nextLine());
- switch (sms) {
- case 2:
- message += "a";
- break;
- case 22:
- message += "b";
- break;
- case 222:
- message += "c";
- break;
- case 3:
- message += "d";
- break;
- case 33:
- message += "e";
- break;
- case 333:
- message += "f";
- break;
- case 4:
- message += "g";
- break;
- case 44:
- message += "h";
- break;
- case 444:
- message += "i";
- break;
- case 5:
- message += "j";
- break;
- case 55:
- message += "k";
- break;
- case 555:
- message += "l";
- break;
- case 6:
- message += "m";
- break;
- case 66:
- message += "n";
- break;
- case 666:
- message += "o";
- break;
- case 7:
- message += "p";
- break;
- case 77:
- message += "q";
- break;
- case 777:
- message += "r";
- break;
- case 7777:
- message += "s";
- break;
- case 8:
- message += "t";
- break;
- case 88:
- message += "u";
- break;
- case 888:
- message += "v";
- break;
- case 9:
- message += "w";
- break;
- case 99:
- message += "x";
- break;
- case 999:
- message += "y";
- break;
- case 9999:
- message += "z";
- break;
- case 0:
- message += " ";
- break;
- }
- }
- System.out.println(message);
- }
- }
- Solution with new switch(Java 21):
- import java.util.Scanner;
- class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine()),
- sms;
- String message = "";
- for (int i = 0; i < n; i++) {
- sms = Integer.parseInt(scanner.nextLine());
- switch (sms) {
- case 2 -> message += "a";
- case 22 -> message += "b";
- case 222 -> message += "c";
- case 3 -> message += "d";
- case 33 -> message += "e";
- case 333 -> message += "f";
- case 4 -> message += "g";
- case 44 -> message += "h";
- case 444 -> message += "i";
- case 5 -> message += "j";
- case 55 -> message += "k";
- case 555 -> message += "l";
- case 6 -> message += "m";
- case 66 -> message += "n";
- case 666 -> message += "o";
- case 7 -> message += "p";
- case 77 -> message += "q";
- case 777 -> message += "r";
- case 7777 -> message += "s";
- case 8 -> message += "t";
- case 88 -> message += "u";
- case 888 -> message += "v";
- case 9 -> message += "w";
- case 99 -> message += "x";
- case 999 -> message += "y";
- case 9999 -> message += "z";
- case 0 -> message += " ";
- };
- }
- System.out.println(message);
- }
- }
- 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()),
- sms;
- String message = "";
- int[] button = { 32, 0, 97, 100, 103, 106, 109, 112, 116, 119 };
- for (int i = 0; i < n; i++) {
- sms = Integer.parseInt(scanner.nextLine());
- message += (char)(button[sms % 10] + Integer.toString(sms).length() - 1);
- }
- System.out.println(message);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement