这是我的代码。我有一个问题。它工作得很好,但是当您将莫尔斯电码转换为英语时,它只会打印出莫尔斯电码单个数字字母。有人可以给我一个解决方案,或者至少帮助我理解哪里出了问题,因为这非常令人沮丧。
这是我的代码中重要的部分
问题的一个例子是当我输入 .-
时它打印了 e
和 t
,而不是 a
.
public class Project1
{
public static void main( String [] args )
{
System.out.println();
choice();
}
public static void choice()
{
int user_choice = 0;
user_choice = Input.getInt("Enter 1 if you want to change English to Morse code, and enter 2 to change Morse code to English");
if(user_choice == 1)
{
String output = new String();
String inital = new String();
inital = english_to_morse();
for( int k = 0; k < inital.length(); k++)
{
output += morse(inital.charAt( k ));
}
System.out.print(output);
}
if(user_choice == 2)
{
String output2 = new String();
String inital2 = new String();
inital2 = morse_to_english();
for( int k = 0; k < inital2.length(); k++)
{
System.out.println("#####"+String.valueOf(inital2.charAt( k ))+"#####");
output2 += english(String.valueOf(inital2.charAt( k )));
}
System.out.print(output2);
}
}
public static String english_to_morse()
{
String user_input = new String();
user_input = Input.getString("Enter a phrase and I'll convert it to Morse Code");
return user_input.toLowerCase();
}
public static String morse_to_english()
{
String user_input = new String();
user_input = Input.getString("Enter a phrase in Morse Code and I'll convert it to English");
return user_input.toLowerCase();
}
public static String morse(char letter)
{
String output = new String();
char[] alphabet_numbers = {'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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' ' };
String morse_code[] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "|" };
for( int j = 0; j < alphabet_numbers.length; j++ )
{
if (alphabet_numbers[j]==letter)
{
output = morse_code[j];
}
}
return output + " ";
}
public static String english(String letter)
{
String output = new String();
String alphabet_numbers[] = {"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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", " " };
String morse_code[] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "|" };
for( int j = 0; j < morse_code.length; j++ )
{
if (morse_code[j].equals(letter))
{
output = alphabet_numbers[j];
}
}
return output + " ";
}
}
最佳答案
.-
给出 et
而不是 a
的原因是你读取字符串的方式。
您的代码读取 .
,然后查找表以确定它是否对应于任何字母字符,在这种情况下它对应:e
。然后你读了一个 -
并查找它,你得到了一个 t
。
如果你的输入只是字面上的
.-.---.-.-.........-----.-.-.-.-
您几乎被困住了,因为您不知道一个何时结束,另一个何时开始。再举个例子,下面的字符串应该怎么区分
.
..
...
....
它们都是同样有效的信号,但根据您对其的解读方式,您会得到截然不同的结果。
你不能说“我只取最长的匹配字符串”之类的话,因为没有理由说这是一个有效的规则。
如果我上面提供的示例输入与您的输入不匹配,您应该指出您的输入是什么。
关于java - 我怎样才能让我的翻译器将摩尔斯电码检测为字符串,而不是字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22799553/