Advertisement
prashandip

NumberCast

Nov 26th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1. public class NumberCast {
  2.  
  3.     public static void main(String[] args) {
  4.         System.out.print("Enter a number between 0 to 35: "); // ask for a number
  5.         Scanner scanner = new Scanner(System.in); // get input
  6.         try { // catch exception when number is not entered
  7.             int n = scanner.nextInt(); // get integer from scanner
  8.             if (n >= 0 && n <= 35) { // n must lie between 0 and 35
  9.                 if (n <= 9) {
  10.                     System.out.println("==> " + n); // if n<9 display as it is
  11.                 } else {
  12.                     // ASCII VALUE OF A = 65
  13.                     System.out.println("==> " + (char) (55 + n)); // adding 55 to n and casting to char if n>9
  14.                 }
  15.             } else {
  16.                 System.out.println("INVALID NUMBER"); // numbers other than those from 0 to 35 are invalid
  17.             }
  18.         } catch (Exception e) {
  19.             System.out.println("NOT A NUMBER"); // "not a number" if number is not entered
  20.         }
  21.         scanner.close(); // close the scanner
  22.     }
  23.  
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement