第六章第二十一题(电话按键盘)(Phone keypads) - 编程练习题答案

*6.21(电话按键盘)国际标准的字母/数字匹配图如编程练习题4.15所示,编写一个方法,返回给定大写字母的数字,如下所示:

int getNumber(char uppercaseLetter)

编写一个测试程序,提示用户输入字符串形式的电话号码。输入的数字可能会包含字母。程序将字母(大写或者小写)翻译成一个数字,然后保持其他字符不变。

*6.21(Phone keypads)The international standard letter/number mapping for telephones is given in Programming Exercise 4.15. Write a method that returns a number, given an uppercase letter, as follows:

int getNumber(char uppercaseLetter)

Write a test program that prompts the user to enter a phone number as a string. The input number may contain letters. The program translates a letter (uppercase or lowercase) to a digit and leaves all other characters intact.

下面是参考答案代码:

// https://cn.fankuiba.com

import java.util.Scanner;

public class Ans6_21_page201 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String phoneNumber = input.nextLine().toUpperCase();
        for (int i =0; i < phoneNumber.length(); i++) {
            if (Character.isDigit(phoneNumber.charAt(i))) {
                System.out.print(phoneNumber.charAt(i));
            }
            else if (getNumber(phoneNumber.charAt(i)) == 1)
                System.out.print("-");
            else
                System.out.print(getNumber(phoneNumber.charAt(i)));
        }
    }
    public static int getNumber(char uppercaseLetter) {
        int num = 99;
        switch (uppercaseLetter+"") {
            case "-":
                num = 1;
                break;
            case "A":
                num = 2;
                break;
            case "B":
                num = 2;
                break;
            case "C":
                num = 2;
                break;
            case "D":
                num = 3;
                break;
            case "E":
                num = 3;
                break;
            case "F":
                num = 3;
                break;
            case "G":
                num = 4;
                break;
            case "H":
                num = 4;
                break;
            case "I":
                num = 4;
                break;
            case "J":
                num = 5;
                break;
            case "K":
                num = 5;
                break;
            case "L":
                num = 5;
                break;
            case "M":
                num = 6;
                break;
            case "N":
                num = 6;
                break;
            case "O":
                num = 6;
                break;
            case "P":
                num = 7;
                break;
            case "Q":
                num = 7;
                break;
            case "R":
                num = 7;
                break;
            case "S":
                num = 7;
                break;
            case "T":
                num = 8;
                break;
            case "U":
                num = 8;
                break;
            case "V":
                num = 8;
                break;
            case "W":
                num = 9;
                break;
            case "X":
                num = 9;
                break;
            case "Y":
                num = 9;
                break;
            case "Z":
                num = 9;
        }
        return num;
    }
}

适用Java语言程序设计与数据结构(基础篇)(原书第11版)Java语言程序设计(基础篇)(原书第10/11版) 更多内容

(Telephone Number Word Generator) Standard telephone keypads contain the digits 0 through 9. The numbers 2 through 9 each have three letters associated with them, as is indicated by the following table: Many people find it difficult to memorize phone numbers, so they use the correspondence between digits and letters to develop seven-letter words that correspond to their phone numbers. For example, a person whose telephone number is 686-2377 might use the correspondence indi- cated in the above table to develop the seven-letter word “NUMBERS.” Businesses frequently attempt to get telephone numbers that are easy for their clients to remember. If a business can advertise a simple word for its customers to dial, then no doubt the business will receive a few more calls. Each seven-letter word corresponds to exactly one seven-digit telephone number. The restaurant wishing to increase its take-home business could surely do so with the number 825-3688 (i.e., “TAKEOUT”). Each seven-digit phone number corresponds to many separate seven-letter words. Unfortunately, most of these represent unrecognizable juxtaposi- tions of letters. It’s possible, however, that the owner of a barber shop would be pleased to know that the shop’s telephone number, 424-7288, corresponds to “HAIRCUT.” A veterinarian with the phone number 738-2273 would be happy to know that the number corresponds to “PETCARE.” Write a program that, given a seven-digit number, writes to a file every possible seven-letter word corresponding to that number. There are 2187 (3 to the seventh power) such words. Avoid phone numbers with the digits 0 and 1.
最新发布
06-09
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值