需要java摩尔斯电码翻译的帮助吗

需要java摩尔斯电码翻译的帮助吗,java,Java,我有一个Java翻译器,当我从英语翻译成摩尔斯语,但不是从摩尔斯语翻译成英语时,它可以工作。 如果你能告诉我,我需要做些什么来翻译莫尔斯,那就太好了。当我输入摩尔斯电码后从摩尔斯电码转到英语时,它只是结束程序,而不是给我翻译 这是我的密码 public class project1 { public static void main ( String [] args ) { char [] english = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',

我有一个Java翻译器,当我从英语翻译成摩尔斯语,但不是从摩尔斯语翻译成英语时,它可以工作。 如果你能告诉我,我需要做些什么来翻译莫尔斯,那就太好了。当我输入摩尔斯电码后从摩尔斯电码转到英语时,它只是结束程序,而不是给我翻译

这是我的密码

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 < b.length(); m++)
                {
                    if (character.equals("inputMorseCode[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" );

    }

}
}
您的计数器位于错误的位置:对于int m=0;m首先,改变这个

    for (int m = 0; m < b.length(); m++)
            {
                if (character.equals("inputMorseCode[m]"))    
                    System.out.print(english[ m ]);    
            }  

因为你应该在莫尔斯数组中搜索莫尔斯字母


这就是说,如果您创建一个映射,并将摩尔斯字符串映射为英语字符,反之亦然,那么您的代码将更加高效。它们将替换您的morse和english数组,并允许您在固定时间内查找英语或morse字母的映射。

添加更多详细信息并定义您面临的问题…尝试更改此行的拆分条件:String[]characters=word.split;这样做:String[]characters=word.split;但我宁愿编写自己的函数来生成char数组,也不愿使用;m for (int m = 0; m < morse.length; m++) { if (character.equals(morse[m])) System.out.print(english[m]); }