【高分悬赏】一道JAVA题,求解~~

题目:选择一组等长的英文单词,例如,一组4个字母组成的单词:workbackcomedealdeskbookjavatoolface一组5个字母组成的单词:watchma... 题目:
选择一组等长的英文单词,例如,一组4个字母组成的单词:
work back come deal desk book java tool face
一组5个字母组成的单词: watch match noise risky stock
试定义一个字符串数组,数组中每个元素储存一个英文单词,元素个数根据选择的英语单词长度而定。再按照电话机表盘定义数字与字母的对应关系,如数字2对应a或b或c,数字5对应j或k或l,现编制一个程序,要求将用户输入的数字转换成相应的字符串(注意一个数字对应多个字符串),将这些字符串与数组中储存的英文单词逐个比较,如果某一个字符串与英文单词匹配成功,则在屏幕上输出数字和字符串及对应的单词;如果都不匹配,则在屏幕上输出一条信息“没有匹配的单词”。
展开
 我来答
leeps_my
2009-10-08 · TA获得超过807个赞
知道小有建树答主
回答量:212
采纳率:0%
帮助的人:0
展开全部
 
 
 
class NumberToWord {
    public static void main(String[] args) {
        String[] 一组等长的英文单词 = "watch match noise risky stock".split(" "),
                数字字母的对应 = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};

        System.out.print("请输入一个数字:");
        String 输入 = new java.util.Scanner(System.in).nextLine().trim();

        try {
            // 将用户输入的数字转换成相应的字符串
            String[] 相应字串组 = {""};
            for (char 一个字符: 输入.toCharArray()) {
                int 一个整数 = Integer.parseInt(Character.toString(一个字符));
                相应字串组 = procreateByTail(相应字串组, 数字字母的对应[一个整数]);
            }

            // 将这些字符串与数组中储存的英文单词逐个比较
            for (String 一个相应字串: 相应字串组)
                for (String 单词: 一组等长的英文单词)
                    // 如果某一个字符串与英文单词匹配成功
                    if (单词.equals(一个相应字串)) {
                        System.out.println(输入 + " 匹配的单词是 " + 单词);
                        return;
                    }

            System.out.println("没有匹配的单词");

        } catch (NumberFormatException nfe) {
            System.out.println("错误:输入并非纯整数");
        }
    }

    // 返回一个含有 heads.length x tails.length() 个字符串的新数组,数组中的
    // 字符串是把 heads 中的每一个字符串和 tails 中的每一个字符逐一连接生成的。
    static String[] procreateByTail(String[] heads, String tails) {
        String[] result = new String[heads.length * tails.length()];
        int i = 0;
        for (String head: heads)
            for (char tail: tails.toCharArray())
                result[i++] = head + tail;

        return result;
    }
}
 
 
 
百度网友0c3258b02
2009-09-30 · TA获得超过672个赞
知道小有建树答主
回答量:550
采纳率:0%
帮助的人:619万
展开全部
没事干,写了个,用树
nodeLevel这个变量后来没用了,
呵呵,本来是想控制树的层数的
层太多了就太慢了:
import java.util.regex.*;
public class WordTree {
private WordTreeNode rootNode;
public WordTree(){

}
public WordTree(String []wordList){
//很多地方都是测试用,wordList大小不能超过int上限
//这个还要控制,程序很多地方没有
for(int i = 0;i<wordList.length;i++){
this.insert(wordList[i]);
}

}
public WordTreeNode getRootNode(){
return this.rootNode;
}
public void setRootNode(WordTreeNode rootNode){
this.rootNode = rootNode;
}
public String []getWords(String s){
String []result = null;
WordTreeNode tempNode = null;
WordTreeNode [] tempNodeList;
int i = 0;
int arrayIndex;
if(this.getRootNode() == null){
return null;
}else{
tempNode = this.getRootNode();
while(i!=s.length()){
System.out.println(i);
tempNodeList = tempNode.getNodeList();
arrayIndex = Integer.parseInt(String.valueOf(s.charAt(i)));
System.out.println("arrayIndex="+arrayIndex);
for(int k = 0;k<tempNodeList.length;k++){
if(tempNodeList[k]!=null)
System.out.println(k+"非空");
}
if(tempNodeList[arrayIndex]==null){
System.out.println("这个是null");
return null;
}else{
System.out.println("tempnode change"+arrayIndex);
tempNode = tempNodeList[arrayIndex];
}
i++;
}
}
String wordList = tempNode.getWordList();
if(wordList == null) return null;
result = wordList.split(",");
return result;
}
public void insert(String s){
if( s == null) return;
if( s.length() == 0) return;
//没有这么长的英文单词
if( s.length() > 200) return;
String regExp = "[a-z|-]*";
Pattern p = Pattern.compile(regExp);
Matcher m = p.matcher(s);
if(!m.find()) return;
//rootNode为空,第一次创建
if(rootNode == null){
int i = 0;
int arrayIndex;
char temp;
WordTreeNode []tempNodeList;
WordTreeNode tempNode = new WordTreeNode();
//用来存放tempNode的子结点
WordTreeNode tempChildNode;
tempNode.setNodeLevel(1);
this.setRootNode(tempNode);
while(i != (s.length())){
temp = s.charAt(i);
arrayIndex = this.getNumber(temp);
tempNodeList = tempNode.getNodeList();
if(tempNodeList[arrayIndex] == null){
tempChildNode = new WordTreeNode();
tempChildNode.setNodeLevel(tempNode.getNodeLevel()+1);
tempNodeList[arrayIndex] = tempChildNode;
tempNode.setNodeList(tempNodeList);
tempNode = tempChildNode;
System.out.println("ggggg"+arrayIndex);
}else{
tempNode = tempNodeList[arrayIndex];
}
i++;
System.out.println("循环"+i+" "+arrayIndex);
}//处理最后一个字符

temp = s.charAt(i-1);
arrayIndex = this.getNumber(temp);
//tempNodeList = tempNode.getNodeList();
System.out.println("root==null;处理最后一个字符"+s.charAt(i-1)+arrayIndex);
tempNode.insert(s);
/**if(tempNodeList[arrayIndex] == null){
tempChildNode = new WordTreeNode();
tempChildNode.setNodeLevel(tempNode.getNodeLevel()+1);
tempNodeList[arrayIndex] = tempChildNode;
tempNode.setNodeList(tempNodeList);
tempChildNode.insert(s);
}else{
tempChildNode = tempNodeList[arrayIndex];
tempChildNode.insert(s);
}*/
//end处理最后一个字符
}else{
int i = 0;
int arrayIndex;
char temp;
WordTreeNode []tempNodeList;
WordTreeNode tempNode = this.getRootNode();
//用来存放tempNode的子结点
WordTreeNode tempChildNode;
//tempNode.setNodeLevel(1);
//this.setRootNode(tempNode);
while(i != (s.length()-1)){
temp = s.charAt(i);
arrayIndex = this.getNumber(temp);
tempNodeList = tempNode.getNodeList();
if(tempNodeList[arrayIndex] == null){
tempChildNode = new WordTreeNode();
tempChildNode.setNodeLevel(tempNode.getNodeLevel()+1);
tempNodeList[arrayIndex] = tempChildNode;
tempNode.setNodeList(tempNodeList);
tempNode = tempChildNode;
}else{
tempNode = tempNodeList[arrayIndex];
}
i++;
}
System.out.println("处理最后一个字符");
//处理最后一个字符
temp = s.charAt(i);
arrayIndex = this.getNumber(temp);
tempNodeList = tempNode.getNodeList();
if(tempNodeList[arrayIndex] == null){
tempChildNode = new WordTreeNode();
tempChildNode.setNodeLevel(tempNode.getNodeLevel()+1);
tempNodeList[arrayIndex] = tempChildNode;
tempNode.setNodeList(tempNodeList);
tempChildNode.insert(s);
System.out.println("insert: "+s);
}else{
tempChildNode = tempNodeList[arrayIndex];
tempChildNode.insert(s);
System.out.println("insert: "+s);
}
//end
}
}
public int getNumber(char c){
int result = 0;
int temp = (int)c;
if(temp == 122){
result = 9;
}else if(temp ==45){
result = 0;
}else if(temp >=112){
switch (c){
case 'p':
case 'q':
case 'r':
case 's':
result = 7;
break;
case 't':
case 'u':
case 'v':
result = 8;
break;
case 'w':
case 'x':
case 'y':
case 'z':
result = 9;
break;
}
}
else{
result = (int)((temp-97)/3) + 2;
}
//System.out.println(c+"返回"+result);
return result;
}

}

public class WordTreeNode {
//这个nodeList定义为几个要看系统实现了
//一般来说,英语单词里面是有 - 连字符的
//除了2-9这8个数字以外,有个连字符可以增加容错性
private WordTreeNode []nodeList;
private int nodeLevel;
private String wordList;
public WordTreeNode(){
nodeList = new WordTreeNode[10];
for(int i = 0;i < nodeList.length;i++){
nodeList[i] = null;
}
wordList = null;
nodeLevel = 0;
}
public int getNodeLevel(){
return this.nodeLevel;
}
public void setNodeLevel(int nodeLevel){
this.nodeLevel = nodeLevel;
}
public WordTreeNode []getNodeList(){
return this.nodeList;
}
public void setNodeList(WordTreeNode []nodeList){
this.nodeList = nodeList;
}
public String getWordList(){
return this.wordList;
}
public void setWordList(String wordList){
this.wordList = wordList;
}
public void insert(String s){
if(this.getWordList() == null){
this.setWordList(s);
}else{
if(this.getWordList().indexOf(s)==-1){
this.setWordList(this.getWordList()+","+s);
}
}
}
}
测试用:
public class MainClass {
public static void main(String []args){
String []s={"my","yaowei","jdk","jdk","jek"};
WordTree w = new WordTree(s);
System.out.println(w.getWords("535")[1]);
}

}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
桐_傲雪
2009-09-28 · TA获得超过555个赞
知道小有建树答主
回答量:354
采纳率:0%
帮助的人:234万
展开全部
整个字符串用split()方法或者stringTokenizer(怎么写的忘了,好像是)分割成多个字符串,这个不用多说了吧。

先翻译每个字符串,将每个字母翻译为数字。用if(abc则为1)-else if(defg则为2)-……-else(都不是则为null)。
再用数字与用户给的数字进行比较。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式