/*
Enter a string: 1-800-Flowers
1-800-3569377
Enter a string: 1800flowers
18003569377
*/
import java.util.Scanner;
public class CountLetter {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = input.nextLine();
char n;
for (int i = 0; i < str.length(); i++) {
if (Character.isLetter(str.charAt(i))) {
n = getNumber(Character.toUpperCase(str.charAt(i)));
str = str.replace(str.charAt(i), n);
}
}
System.out.println(str);
}
public static char getNumber(char up) {
char digit = 'A';
switch (up) {
case 'A':
case 'B':
case 'C':
digit = '2';
break;
case 'D':
case 'E':
case 'F':
digit = '3';
break;
case 'G':
case 'H':
case 'I':
digit = '4';
break;
case 'J':
case 'K':
case 'L':
digit = '5';
break;
case 'M':
case 'N':
case 'O':
digit = '6';
break;
case 'P':
case 'Q':
case 'R':
case 'S':
digit = '7';
break;
case 'T':
case 'U':
case 'V':
digit = '8';
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
digit = '9';
}
return digit;
}
}
Introduction to Java Programming编程题9.7<将字符串中的字符转换为数字>
最新推荐文章于 2022-03-12 21:59:46 发布
本博客介绍了一个使用Java实现的程序,该程序将输入的字母字符串转换为对应的电话号码数字。程序通过定义一个字符到数字的映射表,并遍历输入字符串中的每个字符来完成转换。
102

被折叠的 条评论
为什么被折叠?



