我是Java的新手,我有一个家庭作业,应该在其中编写一个程序,该程序将电话号码转换为与该号码关联的所有可能的字符串。不过,我很难弄清楚这一逻辑。这是我到目前为止所拥有的。任何建议,将不胜感激。

import java.util.*;
public class phoneNumberString{
String[] thisString = {""};
public static void main(String[] args) {


}
public static String dialPad[][] = {
    {"0"}, {"1"}, {"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"}
};

public String[] dial(int number) {

for(int i = 0; i < dialPad.length; i++)

    if(i == 0 ||  i == 1 )
        thisString = new String [] {""};

return thisString;

        }

public static void print(String[] thisString) {
    System.out.println(thisString);
}

}

最佳答案

所以要做的第一件事就是将数字分解成相应的数字...即1234是1、2、3 ...各种解决方案,但最简单的理解方法是通过String操作。

private int[] numberAsDigits(int number) {
    String numberAsString = String.valueOf(number);
    int[] numberAsDigits = new int[numberAsString.length()];
    char[] chars = numberAsString.toCharArray();
    for (int i = 0; i < chars.length; i++) {
        numberAsDigits[i] = Integer.parseInt(String.valueOf(chars[i]));
    }
    return numberAsDigits;
}


现在,您需要遍历所有组合...假设数字是“ 223” ...您的第一个组合将是AAD,然后是BAD,然后是CAD,等等。当您使用CAD时,下一个将是ABD。为了保持位置,您需要一个与数字长度相同的“计数器”数组。

int[] counters = new int[numberAsDigits.length];


因此,您可以通过计数器构建String

// current combination
StringBuilder builder = new StringBuilder();
for (int i = 0; i < counters.length; i++) {
    String[] combosForCurrentDigit= dialPad[numberAsDigits[i]];
    builder.append(combosForCurrentDigit[counters[i]]);
}
results.add(builder.toString());


下一位是将计数器递增到下一个组合。首先添加到第一个,然后“更新”或“保留”更新,直到更新结束,重置并递增下一个更新,即与从99到100的计数相同的逻辑。请注意,我们也需要包括结束条件,这是最后一个计数器超过数组末尾的时间。

// increment the counters
counters[0]++;
for (int i = 0; i < counters.length; i++) {
    String[] combosForCurrentDigit = dialPad[numberAsDigits[i]];
    if (counters[i] == combosForCurrentDigit.length) {
        counters[i] = 0;
        if (i + 1 == counters.length) {
            finished = true;
        } else {
            counters[i + 1]++;
        }
    }
}


全部放在一起

public static void main(String[] args) {
    System.out.println(Arrays.toString(new phoneNumberString().dial(223)));
}

public static String dialPad[][] = { { "0" }, { "1" }, { "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" } };

public String[] dial(int number) {
    int[] numberAsDigits = numberAsDigits(number);

    int[] counters = new int[numberAsDigits.length];
    boolean finished = false;
    List<String> results = new ArrayList<>();
    while (!finished) {
        // current combination
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < counters.length; i++) {
            String[] combosForCurrentDigit = dialPad[numberAsDigits[i]];
            builder.append(combosForCurrentDigit[counters[i]]);
        }
        results.add(builder.toString());

        // increment the counters
        counters[0]++;
        for (int i = 0; i < counters.length; i++) {
            String[] combosForCurrentDigit = dialPad[numberAsDigits[i]];
            if (counters[i] == combosForCurrentDigit.length) {
                counters[i] = 0;
                if (i + 1 == counters.length) {
                    finished = true;
                } else {
                    counters[i + 1]++;
                }
            }
        }
    }

    return results.toArray(new String[0]);
}

private int[] numberAsDigits(int number) {
    String numberAsString = String.valueOf(number);
    int[] numberAsDigits = new int[numberAsString.length()];
    char[] chars = numberAsString.toCharArray();
    for (int i = 0; i < chars.length; i++) {
        numberAsDigits[i] = Integer.parseInt(String.valueOf(chars[i]));
    }
    return numberAsDigits;
}


输出量

[AAD, BAD, CAD, ABD, BBD, CBD, ACD, BCD, CCD, AAE, BAE, CAE, ABE, BBE, CBE, ACE, BCE, CCE, AAF, BAF, CAF, ABF, BBF, CBF, ACF, BCF, CCF]

10-05 21:31