-1

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

输入摩尔斯电码时,每个字母/数字用一个空格分隔,多个单词用“|”分隔。例如 - ---- | -…… 将是句子“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" );

    }

}

}

4

1 回答 1

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 == 中比较字符串是否是相同的对象引用。用于.equals()查看值是否相同。看到这个问题

于 2014-08-23T01:33:10.197 回答