电话号码到字符转换器

时间:2017-05-07 04:37:20

标签: java arrays string for-loop

我是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);
}

}

1 个答案:

答案 0 :(得分:1)

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

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]