*4.15 (Phone key pads) The international standard letter/number mapping found on the telephone is shown below:
Write a program that prompts the user to enter a letter and displays its corresponding number.
Enter a letter: A
The corresponding number is 2
Enter a letter: a
The corresponding number is 2
Enter a letter: +
+ is an invalid input
Write a program that prompts the user to enter a letter and displays its corresponding number.
Enter a letter: A
The corresponding number is 2
Enter a letter: a
The corresponding number is 2
Enter a letter: +
+ is an invalid input
import java.util.Scanner; public class ProgrammingEx4_15 { public static void main(String[] args) { System.out.print("Enter a letter:"); Scanner input = new Scanner(System.in); String s = input.next(); char c = s.toLowerCase().charAt(0); int n; switch (c) { case 'a': case 'b': case 'c': n = 2; break; case 'd': case 'e': case 'f': n = 3; break; case 'g': case 'h': case 'i': n = 4; break; case 'j': case 'k': case 'l': n = 5; break; case 'm': case 'n': case 'o': n = 6; break; case 'p': case 'q': case 'r': case 's': n = 7; break; case 't': case 'u': case 'v': n = 8; break; case 'w': case 'x': case 'y': case 'z': n = 9; //change from n=8; from Akrious7's comment below break; default: n = 0; break; } if (n != 0) { System.out.print("The corresponding number is " + n); System.exit(0); } System.out.print(s + " is an invalid input"); } }
No comments :
Post a Comment