Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class NumberCast {
- public static void main(String[] args) {
- System.out.print("Enter a number between 0 to 35: "); // ask for a number
- Scanner scanner = new Scanner(System.in); // get input
- try { // catch exception when number is not entered
- int n = scanner.nextInt(); // get integer from scanner
- if (n >= 0 && n <= 35) { // n must lie between 0 and 35
- if (n <= 9) {
- System.out.println("==> " + n); // if n<9 display as it is
- } else {
- // ASCII VALUE OF A = 65
- System.out.println("==> " + (char) (55 + n)); // adding 55 to n and casting to char if n>9
- }
- } else {
- System.out.println("INVALID NUMBER"); // numbers other than those from 0 to 35 are invalid
- }
- } catch (Exception e) {
- System.out.println("NOT A NUMBER"); // "not a number" if number is not entered
- }
- scanner.close(); // close the scanner
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement