文本预测文本的递归编程
我想编写一个程序,它接受一组数字(例如 234),并打印出手机键盘上可能存在的所有字母组合。
(1 - 无,2 - abc,3- def 等)
我目前有:
import java.util.*;
public class testCombo {
static String str="217";
static ArrayList<String> list=new ArrayList<String>();
static void addLet(String seg){
if(seg.length()==str.length()){
list.add(seg);
return;
}
char currentChar=str.charAt(seg.length());
if(currentChar==1 || currentChar==0)
{
String str1=seg+" ";
addLet(str1);
}
if(currentChar=='2'){
addLet(seg+"a");
addLet(seg+"b");
addLet(seg+"c");
}
else if(currentChar=='3'){
addLet(seg+"d");
addLet(seg+"e");
addLet(seg+"f");
}
else if(currentChar=='4'){
addLet(seg+"g");
addLet(seg+"h");
addLet(seg+"i");
}
else if(currentChar=='5'){
addLet(seg+"j");
addLet(seg+"k");
addLet(seg+"l");
}
else if(currentChar=='6'){
addLet(seg+"m");
addLet(seg+"n");
addLet(seg+"o");
}
else if(currentChar=='7'){
addLet(seg+"p");
addLet(seg+"q");
addLet(seg+"r");
addLet(seg+"s");
}
else if(currentChar=='8'){
addLet(seg+"t");
addLet(seg+"u");
addLet(seg+"v");
}
else if(currentChar=='9'){
addLet(seg+"w");
addLet(seg+"x");
addLet(seg+"y");
addLet(seg+"z");
}
}
public static void main(String[] args){
addLet("");
for(String str:list) //Sets str to each value in list during each iteration
System.out.println(str);
}
}
作为我的代码,因为我们应该使用递归编程,但我无法让它适用于 1 和 0。 (这只是一个练习课,我还有另一个允许用户输入的课程,它已经验证它只包含数字)
这种查找然后打印出每个组合的方式是否算作递归?
I want to make a program that takes a set of numbers like 234, and at the moment, print out every combination of letters possible that are on a mobile phone keypad.
(1 - nothing, 2 - abc, 3- def and so on)
I currently have:
import java.util.*;
public class testCombo {
static String str="217";
static ArrayList<String> list=new ArrayList<String>();
static void addLet(String seg){
if(seg.length()==str.length()){
list.add(seg);
return;
}
char currentChar=str.charAt(seg.length());
if(currentChar==1 || currentChar==0)
{
String str1=seg+" ";
addLet(str1);
}
if(currentChar=='2'){
addLet(seg+"a");
addLet(seg+"b");
addLet(seg+"c");
}
else if(currentChar=='3'){
addLet(seg+"d");
addLet(seg+"e");
addLet(seg+"f");
}
else if(currentChar=='4'){
addLet(seg+"g");
addLet(seg+"h");
addLet(seg+"i");
}
else if(currentChar=='5'){
addLet(seg+"j");
addLet(seg+"k");
addLet(seg+"l");
}
else if(currentChar=='6'){
addLet(seg+"m");
addLet(seg+"n");
addLet(seg+"o");
}
else if(currentChar=='7'){
addLet(seg+"p");
addLet(seg+"q");
addLet(seg+"r");
addLet(seg+"s");
}
else if(currentChar=='8'){
addLet(seg+"t");
addLet(seg+"u");
addLet(seg+"v");
}
else if(currentChar=='9'){
addLet(seg+"w");
addLet(seg+"x");
addLet(seg+"y");
addLet(seg+"z");
}
}
public static void main(String[] args){
addLet("");
for(String str:list) //Sets str to each value in list during each iteration
System.out.println(str);
}
}
as my code as we are supposed to be using recursive programming, but I can't get it to work for 1s and 0s. (this is only a practice class, I have another one which allows input from user and it already verifies that it only contains digits)
Would this way of finding then printing out every combination count as recursive?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,它是递归的(它通过调用自身来工作),但它不必要地冗长。您可以跳过临时变量,从而节省大量空间,并使其更具可读性。我花了一些时间才明白为什么每种情况下都有几个字符串变量:
WRT 1 和 0,您应该将 currentChar 与
'1'
和'0'
进行比较,而不是1
和0
。Yes it's recursive (it works by calling itself), but it's unnecessarily verbose. You can skip the temporary variables, thus saving a lot of space, and making it more readable. It took me a few moments to grok why you had several string variables in each case:
WRT 1's and 0's, you should be comparing currentChar to
'1'
and'0'
, not1
and0
.您可以通过使用数字到候选字母的映射来简化代码并减少出错的机会:
请注意,这尚未经过测试,但希望您能明白这一点。
You can simplify the code and reduce chances of error by using a mapping of digits to candidate letters:
Note that this is not tested, but hopefully you get the idea.