文本预测文本的递归编程

发布于 2024-09-18 00:49:06 字数 2028 浏览 4 评论 0原文

我想编写一个程序,它接受一组数字(例如 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我恋#小黄人 2024-09-25 00:49:07

是的,它是递归的(它通过调用自身来工作),但它不必要地冗长。您可以跳过临时变量,从而节省大量空间,并使其更具可读性。我花了一些时间才明白为什么每种情况下都有几个字符串变量:

    if(currentChar==1 || currentChar==0)
    {
        addLet(seg+" ");
    }
    if(currentChar=='2'){
        addLet(seg+"a");
        addLet(seg+"b");
        addLet(seg+"c");
    } ...

WRT 1 和 0,您应该将 currentChar 与 '1''0' 进行比较,而不是 10

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:

    if(currentChar==1 || currentChar==0)
    {
        addLet(seg+" ");
    }
    if(currentChar=='2'){
        addLet(seg+"a");
        addLet(seg+"b");
        addLet(seg+"c");
    } ...

WRT 1's and 0's, you should be comparing currentChar to '1' and '0', not 1 and 0.

随梦而飞# 2024-09-25 00:49:07

您可以通过使用数字到候选字母的映射来简化代码并减少出错的机会:

import java.util.*;
import static java.util.Arrays.asList;

public class TestCombo {

    private static Map<Character, List<Character>> lettersByDigit = 
        new HashMap<Character, List<Character>>() {{
           put('0', asList(' '));
           put('1', asList(' '));
           put('2', asList('a', 'b', 'c'));
           put('3', asList('d', 'e', 'f'));

           // and so on - add all candidates per digit in this fashion
        }};

    private static List<String> candidates = new Vector<String>();

    private static void enumerate(String digits, String prefix) {
        if (prefix.length() == digits.length()) {
          candidates.add(prefix);
          return;
        }

        char nextDigit = digits.charAt(prefix.length());

        for (Character nextLetter : lettersByDigit.get(nextDigit)) {
            enumerate(digits, prefix + nextLetter.toString());
        }
    }

    public static void main(String[] args){
        enumerate("217", "");
        for(String candidate : candidates) {
            System.out.println(candidate);
        }
    }
}

请注意,这尚未经过测试,但希望您能明白这一点。

You can simplify the code and reduce chances of error by using a mapping of digits to candidate letters:

import java.util.*;
import static java.util.Arrays.asList;

public class TestCombo {

    private static Map<Character, List<Character>> lettersByDigit = 
        new HashMap<Character, List<Character>>() {{
           put('0', asList(' '));
           put('1', asList(' '));
           put('2', asList('a', 'b', 'c'));
           put('3', asList('d', 'e', 'f'));

           // and so on - add all candidates per digit in this fashion
        }};

    private static List<String> candidates = new Vector<String>();

    private static void enumerate(String digits, String prefix) {
        if (prefix.length() == digits.length()) {
          candidates.add(prefix);
          return;
        }

        char nextDigit = digits.charAt(prefix.length());

        for (Character nextLetter : lettersByDigit.get(nextDigit)) {
            enumerate(digits, prefix + nextLetter.toString());
        }
    }

    public static void main(String[] args){
        enumerate("217", "");
        for(String candidate : candidates) {
            System.out.println(candidate);
        }
    }
}

Note that this is not tested, but hopefully you get the idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文