请教一道题(不是作业,自己练习系罢了)
定义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'}
};
------解决方案--------------------
------解决方案--------------------
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);