卡在java电话号码字生成器上
我的任务是从一个7位数的电话号码中生成每个可能的单词,并使用PrintWriter将其保存为.txt文件。我的代码如下,但我的输出(目前只是打印到控制台)是相同的3个“单词”2187次卡在java电话号码字生成器上,java,phone-number,Java,Phone Number,我的任务是从一个7位数的电话号码中生成每个可能的单词,并使用PrintWriter将其保存为.txt文件。我的代码如下,但我的输出(目前只是打印到控制台)是相同的3个“单词”2187次 package ks2_Lab19; import java.util.Scanner; import java.io.PrintWriter; import java.io.FileNotFoundException; public class WordGenerator { private static
package ks2_Lab19;
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
public class WordGenerator {
private static String[] two = {"a", "b", "c"};
private static String[] three = {"d", "e", "f"};
private static String[] four = {"g", "h", "i"};
private static String[] five = {"j", "k", "l"};
private static String[] six = {"m", "n", "o"};
private static String[] seven = {"p", "r", "s"};
private static String[] eight = {"t", "u", "v"};
private static String[] nine = {"w", "x", "y"};
private static char[] numArray;
private static String[] wordList = new String[2187];
public static void convert(char[] input){
for (int i = 0; i < 2184; i = i + 3){
for (int a = 0; a < 7; a++){
for (int b = 0; b < 3; b++){
if (input[a] == '1' || input[a] == '0') {
wordList[i+b] = wordList[i+b] + " ";
}//if 0 or 1
if (input[a] == '2'){
wordList[i+b] = wordList[i+b] + two[b];
}//if 2
if (input[a] == '3'){
wordList[i+b] = wordList[i+b] + three[b];
}//if 3
if (input[a] == '4'){
wordList[i+b] = wordList[i+b] + four[b];
}//if 4
if (input[a] == '5'){
wordList[i+b] = wordList[i+b] + five[b];
}//if 5
if (input[a] == '6'){
wordList[i+b] = wordList[i+b] + six[b];
}//if 6
if (input[a] == '7'){
wordList[i+b] = wordList[i+b] + seven[b];
}//if 7
if (input[a] == '8'){
wordList[i+b] = wordList[i+b] + eight[b];
}//if 8
if (input[a] == '9'){
wordList[i+b] = wordList[i+b] + nine[b];
}//if 9
}//possible output for loop
}//input array for loop
}//write to wordList for loop
}
public static void main(String[] args) {
//initialize output file name and PrintWriter object
String fileName = "output.txt";
PrintWriter outputStream = null;
String output = "";
//try and catch exception
try {
outputStream = new PrintWriter(fileName);
}
catch (FileNotFoundException e){
System.out.println("Error opening file " + fileName + ".");
System.exit(0);
}
//initialize scanner and wordList array
Scanner kb = new Scanner(System.in);
for (int i=0; i < 2187; i++){
wordList[i] = "";
}
//announce and accept input
System.out.println("Please input a 7 digit phone number without special characters.");
String num = kb.next();
numArray = num.toCharArray();
convert(numArray);
for (int p = 0; p < 2187; p++){
System.out.println(wordList[p]);
}
}
}
包ks2_Lab19;
导入java.util.Scanner;
导入java.io.PrintWriter;
导入java.io.FileNotFoundException;
公共类字生成器{
私有静态字符串[]two={“a”、“b”、“c”};
私有静态字符串[]三={“d”,“e”,“f”};
私有静态字符串[]四={“g”,“h”,“i”};
私有静态字符串[]五={“j”,“k”,“l”};
私有静态字符串[]六={“m”,“n”,“o”};
私有静态字符串[]七={“p”,“r”,“s”};
私有静态字符串[]八={“t”,“u”,“v”};
私有静态字符串[]九={“w”,“x”,“y”};
私有静态字符[]numArray;
私有静态字符串[]字列表=新字符串[2187];
公共静态无效转换(字符[]输入){
对于(int i=0;i<2184;i=i+3){
对于(int a=0;a<7;a++){
对于(int b=0;b<3;b++){
如果(输入[a]=“1”| |输入[a]=“0”){
单词表[i+b]=单词表[i+b]+“”;
}//如果0或1
如果(输入[a]=“2”){
词表[i+b]=词表[i+b]+2[b];
}//如果2
如果(输入[a]=“3”){
词表[i+b]=词表[i+b]+three[b];
}//如果3
如果(输入[a]=“4”){
词表[i+b]=词表[i+b]+4[b];
}//如果4
如果(输入[a]=“5”){
词表[i+b]=词表[i+b]+5[b];
}//如果5
如果(输入[a]=“6”){
词表[i+b]=词表[i+b]+6[b];
}//如果6
如果(输入[a]=“7”){
词表[i+b]=词表[i+b]+seven[b];
}//如果7
如果(输入[a]=“8”){
词表[i+b]=词表[i+b]+8[b];
}//如果8
如果(输入[a]=“9”){
词表[i+b]=词表[i+b]+9[b];
}//如果9
}//循环的可能输出
}//循环输入数组
}//写入循环的单词列表
}
公共静态void main(字符串[]args){
//初始化输出文件名和PrintWriter对象
字符串fileName=“output.txt”;
PrintWriter outputStream=null;
字符串输出=”;
//尝试捕获异常
试一试{
outputStream=新的PrintWriter(文件名);
}
catch(filenotfounde异常){
System.out.println(“打开文件“+fileName+”时出错”);
系统出口(0);
}
//初始化扫描程序和字列表数组
扫描仪kb=新扫描仪(System.in);
对于(int i=0;i<2187;i++){
字表[i]=“”;
}
//宣布并接受输入
System.out.println(“请输入一个没有特殊字符的7位电话号码”);
字符串num=kb.next();
numArray=num.toCharArray();
皈依(努马拉);
对于(int p=0;p<2187;p++){
System.out.println(wordList[p]);
}
}
}
我相信下面的代码正确地解决了问题,并演示了尾部递归的使用,这听起来像是本练习的目标。还请注意,我正在使用集合库中的各种项,而不仅仅是使用原始数组和大型if/then/else块
import java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
公共类字生成器{
私有静态地图;
静止的{
digitMap=newhashmap();
digitMap.put(Character.valueOf('0'),新字符[]{''});
digitMap.put(Character.valueOf('1'),新字符[]{''});
digitMap.put(Character.valueOf('2'),新字符[]{'a','b','c'});
digitMap.put(Character.valueOf('3'),新字符[]{'d','e','f'});
digitMap.put(Character.valueOf('4'),新字符[]{'g','h','i'});
digitMap.put(Character.valueOf('5'),新字符[]{'j','k','l'});
digitMap.put(Character.valueOf('6'),新字符[]{'m','n','o'});
digitMap.put(Character.valueOf('7'),新字符[]{'p','r','s'});
digitMap.put(Character.valueOf('8'),新字符[]{'t','u','v'});
digitMap.put(Character.valueOf('9'),新字符[]{'w','x','y'});
}
公共静态void转换(字符串输入、字符串结果OFAR、列出所有结果){
if(input.length()==0){
//我们已经到达了输入电话号码的末尾,因此
//递归
allResults.add(resultSoFar);
}否则{
//去掉电话号码前面的下一个字符
Character nextDigit=Character.valueOf(input.charAt(0));
//查找从该数字到所有字母的映射列表
char[]mappingArray=digitMap.get(nextDigit);
//更健壮的错误处理会引发异常或
//在中遇到未知字符时发生的其他情况
//电话号码。
if(mappingArray!=null){
//我们已经处理了剩余数字中的第一个数字,
//因此,与数字的其余部分一起递归
字符串inputTail=input.substring(1);
//通过对数组进行迭代,映射列表不会全部显示
//大小必须相同。
for(char nextLetter:mappingArray){
//将下一个映射的字母放在所选结果的末尾
//构建并递归
转换(inputMail、resultSoFar+nextLetter、allResults);
}
}
}
}
公共静态void main(字符串[]args){
//不要求输入的简化版本
字符串num=“8675309”;
列出结果
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class WordGenerator {
private static Map<Character, char[]> digitMap;
static {
digitMap = new HashMap<Character, char[]>();
digitMap.put(Character.valueOf('0'), new char[] { ' ' });
digitMap.put(Character.valueOf('1'), new char[] { ' ' });
digitMap.put(Character.valueOf('2'), new char[] { 'a', 'b', 'c' });
digitMap.put(Character.valueOf('3'), new char[] { 'd', 'e', 'f' });
digitMap.put(Character.valueOf('4'), new char[] { 'g', 'h', 'i' });
digitMap.put(Character.valueOf('5'), new char[] { 'j', 'k', 'l' });
digitMap.put(Character.valueOf('6'), new char[] { 'm', 'n', 'o' });
digitMap.put(Character.valueOf('7'), new char[] { 'p', 'r', 's' });
digitMap.put(Character.valueOf('8'), new char[] { 't', 'u', 'v' });
digitMap.put(Character.valueOf('9'), new char[] { 'w', 'x', 'y' });
}
public static void convert(String input, String resultSoFar, List<String> allResults) {
if (input.length() == 0) {
// We have hit the end of the input phone number and thus the end of
// recursion
allResults.add(resultSoFar);
} else {
// Strip the next character off the front of the phone number
Character nextDigit = Character.valueOf(input.charAt(0));
// Look up the list of mappings from that digit to all letters
char[] mappingArray = digitMap.get(nextDigit);
// More robust error handling would throw an exception or do
// something else when an unknown character was encountered in the
// phone number.
if (mappingArray != null) {
// We have processed the first digit in the rest of the number,
// so recurse with the rest of the number
String inputTail = input.substring(1);
// By iterating through the array the mapping lists do not all
// have to be the same size.
for (char nextLetter : mappingArray) {
// Put the next mapped letter on the end of the result being
// built and recurse
convert(inputTail, resultSoFar + nextLetter, allResults);
}
}
}
}
public static void main(String[] args) {
// Simplified version that does not ask for input
String num = "8675309";
List<String> results = new ArrayList<String>();
// Starting condition is that the entire input needs to be processed,
// the result so far is empty, and we have nothing in the list of final
// answers
convert(num, "", results);
for (String nextResult : results) {
System.out.println(nextResult);
}
System.out.println("End of results list. Total words generated: " + results.size());
}
}