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



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

输入莫尔斯电码时,每个字母/数字之间用一个空格隔开,多个单词之间用"|"分隔。例如:- --- | -... .将是输入句子"to be"的摩尔斯电码。你的程序只需要处理一个句子,并且可以忽略标点符号。

输入英文时,每个单词之间用空格分隔。

我在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" );
    }
}

}

通过莫尔斯电码输入迭代的代码并没有按照您的想法执行。尽管要求用户输入带有分隔符的多个字符序列,但您正在遍历字符串,提取单个字符。

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中==比较如果字符串是相同的对象引用。使用.equals()查看值是否相同。

最新更新