Advertisement
CR7CR7

msg

Oct 12th, 2022
628
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Messages {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         String message = "";
  7.         int numberOfInputs = Integer.parseInt(scanner.nextLine());
  8.  
  9.         for (int i = 0; i < numberOfInputs; i++) {
  10.             int input = Integer.parseInt(scanner.nextLine());
  11.  
  12.             //Find the number of digits the input has “e.g. 222  3 digits”
  13.             int numOfDigits = Integer.toString(input).length();
  14.  
  15.             //Find the main digit of the input “e.g.  222  2”
  16.             int mainDigit = input % 10;
  17.  
  18.             //Find the offset of the number
  19.             int offset = (mainDigit - 2) * 3;
  20.             if (mainDigit == 8 || mainDigit == 9) offset++;
  21.  
  22.             //Find the letter index
  23.             int letterIndex = offset + numOfDigits - 1;
  24.  
  25.             //and add that to the ASCII code of the lowercase letter “a” (97)
  26.             if (mainDigit == 0) {
  27.                 message += " ";
  28.             } else {
  29.                 message += String.valueOf(Character.toChars(letterIndex + 97));
  30.             }
  31.         }
  32.         System.out.println(message);
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement