该程序将从telephones.txt文件中读取每个电话号码,并检查它是否可以翻译成一个或多个words.txt文件。该程序的输出将包含电话号码和他们的单词代表。 (假设有字和电话在文件中)字符串不相等
while(phones.hasNext())
{
number = phones.next();
number = number.replace("-","");
//Scans for next word string
System.out.print(number + " ");
Scanner words = new Scanner(new File("words.txt"));
while(words.hasNext())
{
code = words.next();
char[] wordChars = null;
wordChars = code.toCharArray();
output = "";
for(char wordChar : wordChars)
{
output = output.concat(new String(convert(wordChar)));
}
if(number.equals(output))
{
end += code;
System.out.print(code);
if(end.isEmpty() && end !=null)
{
System.out.print("NONE");
}
}
}
System.out.println();
}
}
假设该文件中的字被CBCNEWS,CTVNEWS和数字是2251493和2226397 如果(number.equals(输出)); System.out.println(number +“”+ code); } 此代码仅在字符串编号等于字符串输入时才打印,而始终打印。什么条件我不得不作出这样,当满足条件时,将只打印,也将继续寻找更多的话
我如何转换的串码是用这样的:
public static char[] convert(char c)
{
switch(c)
{
case 'A' :
case 'B' :
case 'C' :
return new char[]{'2'};
case 'D' :
case 'E' :
case 'F' :
return new char[]{'3'};
case 'G' :
case 'H' :
case 'I' :
return new char[]{'4'};
case 'J' :
case 'K' :
case 'L' :
return new char[]{'5'};
case 'M' :
case 'N' :
case 'O' :
return new char[]{'6'};
case 'P' :
case 'R' :
case 'S' :
return new char[]{'7'};
case 'T' :
case 'U' :
case 'V' :
return new char[]{'8'};
default:
return new char[]{'9'};
}
}
与问题无关,但我会建议用'HashMap'替换'convert'方法。 – Smallhacker