gpt4 book ai didi

java - 电话号码到字符转换器

转载 作者:行者123 更新时间:2023-11-30 06:45:00 27 4
gpt4 key购买 nike

我是 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...各种解决方案,但最容易理解的解决方案是通过字符串操作,无需数学即可理解。

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];

所以你从计数器构建你的字符串

// 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]

关于java - 电话号码到字符转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43827870/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com