java - 我怎样才能让我的翻译器将摩尔斯电码检测为字符串,而不是字符

标签 java

这是我的代码。我有一个问题。它工作得很好,但是当您将莫尔斯电码转换为英语时,它只会打印出莫尔斯电码单个数字字母。有人可以给我一个解决方案,或者至少帮助我理解哪里出了问题,因为这非常令人沮丧。

这是我的代码中重要的部分

问题的一个例子是当我输入 .- 时它打印了 et,而不是 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/

相关文章:

java - 为什么 ant 中的 javac 使用不兼容的类型构建脚本错误

java - 如何删除字符串中特定位置的数值

java - Java 中的作用域和数组是如何工作的?

java - 如何在不使用文件路径的情况下引用图像?

java - Android Studio 2.0 使用带有 jar 和源文件的外部项目

java - 你可以专门化继承的Java注释吗

java - 无法从 GAE 的 java mail APi 发送邮件

java - 如何使用类加载器加载 apach xmlbeans 类?

java - 如何更改另一个类中变量的值,并且仍然能够在另一个类中更改它?

java - 使用自签名 SSL 证书的 UDP 服务器