首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

请问一道题(不是作业,自己练习系罢了)

2011-12-28 
请教一道题(不是作业,自己练习系罢了)定义4个字母的字符串数组:work、back、come、deal、desk、book、Java、tool、

请教一道题(不是作业,自己练习系罢了)
定义4个字母的字符串数组:work、back、come、deal、desk、book、Java、tool、face
试定义一个字符串数组,数组中每个元素存储一个英文单词,元素个数根据选择的英语单词长度而定。再按照电话机表盘定义的数字与字母的对应关系,如数字2对应a或b或c
,数字5对应j或者k或者l。现编制一个程序,要求将用户输入的数字串转换成相应的字符串(注意一个数字串对应多个字符串),将这些字符串与数组中存储的英文单词逐个比较,如果某一字符串与与英文单词匹配成功,则在屏幕上输出数字串以及对应的单词,如果都不匹配,则在屏幕上输出一条信息“没有匹配的单词”。

我看了一下,定义一个字符串,那怎么把数字和字母匹配起来呢?不知道数组中有没有键-值这种对应的匹配方法。请大家讲一下这道题解题思路,谢谢了。

[解决办法]
像手机输入法一样

你第一个数字假设是对应abc, 那么你首先扫描你的英文数组, 抛弃那些非abc打头的, 如此类推下去,
[解决办法]
定义这样一个数组:

Java code
char[][] charSet={null,null,                 {'a','b','c'},                 {'d','e','f'},                 {'g','h','i'},                 {'j','k','l'},                 {'m','n','o'},                 {'p','q','r','s'},                 {'t','u','v'},                 {'w','x','y','z'}                           };
[解决办法]
探讨
引用:
像手机输入法一样

你第一个数字假设是对应abc, 那么你首先扫描你的英文数组, 抛弃那些非abc打头的, 如此类推下去,



有道理..UP..

[解决办法]
package topchina;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Phonekey extends JFrame implements ActionListener {
public JButton[] jb;
JButton jbutton;
boolean isExit=false;
public JLabel jl;
int count=0;
String array[] = { "work", "back", "come", "deal", "desk", "book", "Java",
"tool", "face" };
String result = "";
String temp1[],temp2[],temp3[],temp4[];
String temp[];
String keydown="";
String dd="";
String key1[] = { "a", "b", "c" };
String key2[] = { "d", "e", "f" };
String key3[] = { "g", "h", "i" };
String key4[] = { "j", "k", "l" };
String key5[] = { "m", "n", "o" };
String key6[] = { "p", "q", "r" };
String key7[] = { "s", "t", "u" };
String key8[] = { "v", "w", "x" };
String key9[] = { "y", "z", "0" };

public Phonekey() {
super("电话模拟输出");
this.setLayout(null);
jb = new JButton[10];
jbutton=new JButton("清空");
for (int i = 0; i < 10; i++) {
jb[i] = new JButton("" + i);
if (i < 5) {
jb[i].setBounds(10 + 50 * i, 0, 50, 50);
} else {
jb[i].setBounds(10 + 50 * (i - 5), 70, 50, 50);
}
jb[i].addActionListener(this);
this.add(jb[i]);
}
jbutton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
count=0;
keydown="";
jl.setText("请输入吧...");
}

});
jbutton.setBounds(350,230, 50, 50);
this.add(jbutton);
jl = new JLabel("请输入吧...");
jl.setBackground(Color.red);
jl.setBounds(50, 140, 300, 140);
temp=new String[3];temp1=new String[3];temp2=new String[3];temp3=new String[3];temp4=new String[3];
this.add(jl);
this.setSize(400, 300);
this.setVisible(true);
}

public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);


new Phonekey();
}

public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JButton jbh = (JButton) e.getSource();
keydown+=jbh.getText();
if (jbh.getText().equals("1")) {
temp = key1;
} else if (jbh.getText().equals("2")) {
temp = key2;
} else if (jbh.getText().equals("2")) {
temp = key2;
} else if (jbh.getText().equals("3")) {
temp = key3;
} else if (jbh.getText().equals("4")) {
temp = key4;
} else if (jbh.getText().equals("5")) {
temp = key5;
} else if (jbh.getText().equals("6")) {
temp = key6;
} else if (jbh.getText().equals("7")) {
temp = key7;
} else if (jbh.getText().equals("8")) {
temp = key8;
} else if (jbh.getText().equals("9")) {
temp = key9;
}
if(count==0){
temp1=temp;
}else if(count==1){
temp2=temp;
}else if(count==2){
temp3=temp;
}else if(count==3){
temp4=temp;
}
if(count==3){
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < 3; j++) {
for (int j2 = 0; j2 <3; j2++) {
for (int k = 0; k < 3; k++) {
for (int k2 = 0; k2 < 3; k2++) {
if(array[i].equals(temp1[j]+temp2[j2]+temp3[k]+temp4[k2])){
jl.setText(array[i]+"数字"+keydown);
isExit=true;
}
}
}
}
}

}
}
if(false==isExit&& count==3){
jl.setText("没有匹配的单词");
}
if(count==2){
isExit=false;
}
System.out.println(count);
count++;
if(count==4){
count=0;
keydown="";

}
//System.out.println(jbh.getText());
}
}

[解决办法]
为什么不用hashMap,虽然效率上合数组比可能会差些,但是方便维护,更加符合设计模式

热点排行