Java编程 高手来啊 救急啊!

来源:百度知道 编辑:UC知道 时间:2024/06/30 23:48:49
选择一组等长的英文单词,例如,一组4个字母组成的单词:
work back come deal desk book java tool face
一组5个字母组成的单词:
watch match noise risky stock
试定义一个字符串数组,数组中每个元素存储一个英文单词,元素个数根据选择的英语单词长度而定。再按照电话机表盘定义数字与字母的对应关系,如数字2对应a或b或C,数字5对应j或k或1。现编制一个程序,要求将用户输入的数字串转换成相应的字符串〔注意一个数字串对应多个字符串),并将这些字符串与数组中存储的英文单词逐个比较。如果某一字符串与英文单词匹配成功,则在屏幕上输出数字串及对应的单词;如果都不匹配,则在屏幕上输出一条信息“there is no result" .

public static void main(String[] args) {
String[] arrs = new String[] { null, null, "abc", "def", "ghi", "jkl",
"mno", "pqrs", "tuv", "wxyz" };
String arg1 = "watch match noise risky stock"; // 你要对比的字符串
String arg2 = "92824 62824 66473 74759 78625"; // 你输入的数字串

boolean bool = true;
int length = 0;
while (length < arg2.length()) {
String string1 = arg1.substring(length, length + 1); // 防止数组越界
String string2 = arg2.substring(length, length + 1);
length++;
if (string2.equals(" "))
continue;
int number = Integer.parseInt(string2); // 可以做多判断,以免异常发生
String arg = arrs[number];
if (arg != null)
if (arg.indexOf(string1) == -1) {
bool = false;
break;
}

}
if (bool)
System.out.println(arg1);