Java Programming and Data Structures(11th Edition) 第四章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 lowercase or uppercase letter and displays its corresponding number. For a nonletter input, display invalid input.
代码:
package four;
import java.util.Scanner;
public class exam15 {
public static void main(String[] args) {
System.out.println("Enter a letter:");
int i=0, j;
char char1;
boolean n=false;
Scanner input = new Scanner(System.in);
char1 = input.next().charAt(0);
String []r = {"abcABC2", "defDEF3", "ghiGHI4", "jklJKL5", "mnoMNO6", "pqrsPQRS7", "tuvTUV8", "wxyzWXYZ9"};
for (i = 0; i < r.length; i++){
for (j = 0; j < r[i].length(); j++)
if (char1 == r[i].charAt(j)) {
n = true;
break;
}
if(n)
break;}
if (n = true)
System.out.println("The corresponding number is" + r[i].charAt(r[i].length() - 1));
else
System.out.println(" is an invalid input");
}
}