将英语翻译成摩尔斯电码,反之亦然

时间:2014-08-23 01:20:37

标签: java morse-code

该项目涉及编写一个程序,将莫尔斯电码翻译成英语和英语为莫尔斯电码。您的程序将提示用户指定所需的翻译类型,输入一串莫尔斯电码字符或英文字符,然后显示翻译结果。

输入摩尔斯电码时,用单个空格分隔每个字母/数字,并用“|”分隔多个单词。例如, - --- | - ......将是“将来”的摩尔斯电码输入。您的程序只需处理一个句子,可以忽略标点符号。

输入英语时,请用空格分隔每个单词。

我得到了一个无法比拟的类型:if(Morse [m] == b.charAt(m))行中的字符串和字符错误。有想法该怎么解决这个吗?谢谢!

public class project {

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 == "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 | ." );

        for ( int x = 0; x < Morse.length; x++ )
        {
            for ( int m = 0; m < b.length (); m++ )
            {
                if ( Morse [ m ] == b.charAt ( m ) )

                System.out.print ( English [ m ] + " " );

            }

        }

    }

    else if ( a == "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" );

    }

}

}

1 个答案:

答案 0 :(得分:2)

这段代码迭代了莫尔斯代码输入并没有按照你的想法去做。尽管要求用户输入带分隔符的多个字符序列,但您正在迭代字符串中拉出单个字符。

if ( a == "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 | ." );    

        for ( int x = 0; x < Morse.length; x++ )
        {
            for ( int m = 0; m < b.length (); m++ )
            {
                if ( Morse [ m ] == b.charAt ( m ) )    
                System.out.print ( English [ m ] + " " );    
            }    
        }    
    }

更改它以便它实际上循环通过单独的莫尔斯电码

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])))    //This line is no longer failing because a string is being compared to a string
                        System.out.print(English[ m ]);    
                }    
            }
            System.out.print(" ");    
        }    
    }

同样在Java中==比较string是否是同一个对象引用。使用.equals()查看值是否相同。 See this question