做了一下午都没做出来的JAVA题 请高手帮忙 谢谢了

来源:百度知道 编辑:UC知道 时间:2024/06/27 20:23:37
选择一组等长的英文单词,例如,一组4个字母组成的单词:
word back come deal desk book java tool face

一组5个字母组成的单词:

watch match noise risky stock

试定义一个字符串数组,数组中的每个元素存储一个英文单词,元素个数根据选择的英语单词长度而定。再按照电话机表盘定义数字与字母的对应关系,如数字2对应a或b或c ,数字5对应j或k或l。现编制一个程序,要求将用户输入的数字串转换成相应的字符串(注意一个数字串对应多个字符串)并将字符串与数组中存储的英文单词逐个比较。如果某一字符串与英文单词匹配成功,则在屏幕上输出数字串及对应的单词,如果都不匹配,则在屏幕上输出一条信息"没有匹配的单词"

import java.util.HashMap;
import java.util.Scanner;

public class Test {

public static void main(String args[]) {
String s = "word back come deal desk book java tool face" + " " + "watch match noise risky stock";
String[] sa = s.split(" ");
HashMap<String, String> hm = new HashMap<String, String>();
for(String sx : sa)
hm.put(numberOf(sx), sx);
String word = hm.get(new Scanner(System.in).next());
System.out.println(word == null ? "没有匹配的单词" : word);
}
private static String numberOf(String s)
{
String result = "";
for(int i = 0; i < s.length(); i++)
switch(s.charAt(i))
{
case 'a': case 'b': case 'c': result += "2"; break;
case 'd': case 'e': case 'f': result += "3"; bre