【问题标题】:Need assistance with java morse code translator only translating one symbol需要 Java 莫尔斯电码翻译器的帮助,只翻译一个符号
【发布时间】:2015-05-31 05:17:08
【问题描述】:

我刚刚制作了一个 java 莫尔斯电码翻译器,但是当我将莫尔斯电码转换为英文时,它会将每个符号转换为一个字母,所以当我翻译它时......它给了我 e e e e 而不是 h。所以我的问题是如何做到这一点,以便我可以用摩尔斯电码翻译整个单词并使用 |分隔单词。

这是我的代码

public class project1 {

public static void main ( String [] args ) {

char [] english = { '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', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };

String [] morse = { ".-" , "-..." , "-.-." , "-.." , "." , "..-." , "--." , "...." , ".." , ".---" , "-.-" , ".-.." , "--" , "-." , "---" , ".--." , "--.-" ,  ".-." , "..." , "-" , "..-" , "...-" , ".--" , "-..-" , "-.--" , "--.." , "|" };
    String a = Input.getString ( "Please enter MC if you want to translate Morse Code into English, or Eng if you want to translate from English into Morse Code" );
if (a.equals("MC"))
    {
        String b = Input.getString ("Please enter a sentence in Morse Code. Separate each letter/digit with a single space and delimit multiple words with a | .");    

        String[] words = b.split("|");
        for (String word: words )
        {
            String[] characters = word.split(" ");
            for (String character: characters) 
            {
                if (character.isEmpty()) { continue; }
        for (int m = 0; m < morse.length; m++)
                {
                    if (character.equals(morse[m]))    
                        System.out.print(english[m]);    
                }    
            }
            System.out.print(" ");    
        }    
    }
else if (a.equals("Eng"))
    {
        String c = Input.getString ( "Please enter a sentence in English, and separate each word with a blank space." );

        c = c.toLowerCase ();

        for ( int x = 0; x < english.length; x++ )
        {
            for ( int y = 0; y < c.length (); y++ )
            {
                if ( english [ x ] == c.charAt ( y ) )

                System.out.print ( morse [ x ] + "  " );


            }

        }


    }

    else 
   {
       System.out.println ( "Invalid Input" );

    }

}
}

【问题讨论】:

    标签: java morse-code


    【解决方案1】:

    您遇到的一个问题是您的String#split(...) 方法没有考虑到| 是关于正则表达式的特殊字符,并且没有像您期望的那样拆分字符串。您需要转义此字符才能正常工作。所以改变这个:

    String[] words = b.split("|");
    

    到这里:

    String[] words = b.split("\\|"); // escaping the pipe char
    

    要调试并查看是否按预期工作,请使用调试器或在代码中添加 println,例如:

     String[] words = b.split("\\|");
    
     // TODO: remove line below from finished program
     System.out.println(java.util.Arrays.toString(words)); 
    

    顺便说一句,如果这是我的程序,我会使用 HashMap&lt;String, String&gt; 来帮助我将英语翻译成摩尔斯,反之亦然。

    在我刚刚执行的进一步搜索中,请查看:Why does String.split need pipe delimiter to be escaped?,了解有关为什么需要转义管道字符的背景信息。

    【讨论】:

    • 这并没有解决java只将每个点和破折号视为个体的问题
    • @eliwinston:在你的 morse-to-english 代码的几个位置添加我建议的调试代码——看看你的程序运行时发生了什么,向我们展示你的输入文本和你的调试输出,因为更改对我来说效果很好。
    【解决方案2】:

    这是您改编的代码,可以正常工作...

    import java.util.Scanner;
    
    public class project1 {
    
    public static void main ( String [] args ) {
    
    char [] english = { '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', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
    
    String [] morse = { ".-" , "-..." , "-.-." , "-.." , "." , "..-." , "--." , "...." , ".." , ".---" , "-.-" , ".-.." , "--" , "-." , "---" , ".--." , "--.-" ,  ".-." , "..." , "-" , "..-" , "...-" , ".--" , "-..-" , "-.--" , "--.." , "|" };
    
    Scanner scanner = new Scanner(System.in);
    System.out.println("Please enter MC if you want to translate Morse Code into English, or Eng if you want to translate from English into Morse Code");
    String a = scanner.nextLine();
    
    if (a.equals("MC"))
        {
            System.out.println("Please enter a sentence in Morse Code. Separate each letter/digit with a single space and delimit multiple words with a | .");    
    
            String b = scanner.nextLine();
    
            String[] words = b.split("|");
            for (String word: words )
            {
                String[] characters = word.split(" ");
                for (String character: characters) 
                {
                    if (character.isEmpty()) { continue; }
            for (int m = 0; m < morse.length; m++)
                    {
                        if (character.equals(morse[m]))    
                            System.out.print(english[m]);    
                    }    
                }
                System.out.print(" ");    
            }    
        }
    else if (a.equals("Eng"))
        {
            System.out.println("Please enter a sentence in English, and separate each word with a blank space.");
            String c = scanner.nextLine();
    
            c = c.toLowerCase ();
    
            for ( int x = 0; x < english.length; x++ )
            {
                for ( int y = 0; y < c.length (); y++ )
                {
                    if ( english [ x ] == c.charAt ( y ) )
    
                    System.out.print ( morse [ x ] + "  " );
    
    
                }
    
            }
    
    
        }
    
        else 
       {
           System.out.println ( "Invalid Input" );
    
        }
    
    }
    }
    

    【讨论】:

    • 请不要只为他工作。如果你解释这些变化会更好。更好的是解释他需要做什么来进行更改,然后让他创建自己的更正代码。
    • 这并没有解决问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    • 2014-09-18
    • 2021-10-05
    相关资源
    最近更新 更多