1

モールス信号トランスレータに関する多くの質問が飛び交うのを見てきましたが、それらの多くを見てきましたが、提案された回答はすべて、同じ間違った出力を与えてくれます。コードの背後にあるアイデアは、配列を使用してモールス信号を英語に、またはその逆に変換できるようにすることです。私のコードは次のとおりです。

import java.util.Scanner;
public static void main ( String [] args )
{

    String [] morse = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "|"};

    String [] 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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", " "};

    Scanner input = new Scanner ( System. in );
    System.out.println ( "Enter 1 to translate Morse Code to English or 2 to translate English to Morse Code:");
    int userChoice = input.nextInt();
    String translateMe;

    while (userChoice < 1 || userChoice > 2 ) // Ensures user enters a valid choice
    {
        System.out.println( "Not a valid entry. Enter 1 to translate Morse Code to English or 2 to translate English to Morse Code:");
        userChoice = input.nextInt();
    }

   if (userChoice == 1 )
    {
        System.out.println("Please enter a Morse Code statement to translate. Separate letters with spaces and words with a ' | '" );
        translateMe = input.next();

        String [] morseChar = translateMe.split(" ");

        for( int x = 0; x < morseChar.length; x++)
        {
            String letter = morseChar[ x ];
            for ( int index = 0; index < morse.length; index++ )
            {
                if(morse [ index ].equals(letter))
                {
                    System.out.print(english[ index ]); // Display character at matching index position of English array to show translation
                }
            }
        }

        }

    else
    {
        System.out.println("Please enter an English statement to translate:");
        translateMe = input.next();
        translateMe = translateMe.toLowerCase();

        String [] englishChar = translateMe.split("(?!^)");

        for ( int x = 0; x < englishChar.length; x++)
        {
            String letter = englishChar [ x ];

            for (int index = 0; index < english.length; index++)
            {
                if( english [index].equals( letter ))
                {
                    System.out.print(morse[index] + " "); // Display Morse Code array character at matching index position to show translation
                }

            }
        }

    }

}

}

という言葉を使ってきました

to be

およびそのモールス符号の対応物

- --- | -… .

テストフレーズとして。このフレーズで英語をモールス符号に翻訳しようとすると、

... -. s と n

出力として。モールス符号を英語にしようとすると、

u

出力として。2 つの文字列配列を確認morse[A]english[A]て、同じインデックス位置などにあることを確認しましたが、問題ありません。この問題の原因となるものは他に考えられません。

編集:IntelliJ IDEA 15を使用していることを知っておくと役立つ場合があります

4

4 に答える 4

1
  1. n( )のコードが"-."配列に 2 回表示されます。
  2. String を に変換した方がよいでしょうchar[]String[]アルファベットを からに変更する必要がありますchar[]
  3. これが私たちが持っている理由Mapです。
于 2016-02-26T02:25:12.790 に答える
1

スキャナーを使用しているため、モールスから英語への翻訳が機能していません。次のように nextLine() を使用する必要があります。

if (userChoice == 1 )
{
    translateMe = input.nextLine();
    System.out.println("Please enter a Morse Code statement to translate. Separate letters with spaces and words with a ' | '" );
    translateMe = input.nextLine();

その後、分割コマンドを使用しても、うまく翻訳されているようです。

出力:

[.-, -..., -.-.]
abc

出力 2:

Please enter a Morse Code statement to translate. Separate letters with spaces and words with a ' | '
- --- | -... .
[-, ---, |, -..., .]
to be
于 2016-02-26T02:36:29.463 に答える
0

英語の翻訳から「s」と「n」を取得している理由は、「to」という単語を調べると、アルファベットの「t」の前の文字が「s」であり、アルファベットの前の文字が「o」であるためです。 " は "n" であるため、そこにループ エラーがあると思われます。さらに、「nextLine()」の代わりに「next()」を使用しているため、自分が何を考えているかを解析していません。「nextLine()」を使用してループを修正すると、それが得られると思います。完全なソース コードを提供していただきありがとうございます。:D

これが私がテストした方法です:

import java.util.Scanner;

パブリック クラス メイン {

public static void main ( String [] args )
{

    String [] morse = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "|"};

    String [] 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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", " "};

    Scanner input = new Scanner ( System. in );
    System.out.println ( "Enter 1 to translate Morse Code to English or 2 to translate English to Morse Code:");
    int userChoice = input.nextInt();
    String translateMe;

    while (userChoice < 1 || userChoice > 2 ) // Ensures user enters a valid choice
    {
        System.out.println( "Not a valid entry. Enter 1 to translate Morse Code to English or 2 to translate English to Morse Code:");
        userChoice = input.nextInt();
    }

    if (userChoice == 1 )
    {
        System.out.println("Please enter a Morse Code statement to translate. Separate letters with spaces and words with a ' | '" );
        translateMe = input.next();

        String [] morseChar = translateMe.split(" ");

        for( int x = 0; x < morseChar.length; x++)
        {
            System.out.println("Comparing " + morseChar + " to the morse array");
            for ( int index = 0; index < morse.length; index++ )
            {
                System.out.println("Comparing equality of: " + morseChar[x] + " and " + morse[index]);
                if(morseChar[x].equals(morse[index]))
                {
                    System.out.print(english[ index ]); // Display character at matching index position of English array to show translation
                }
            }
        }

    }

    else
    {
        System.out.println("Please enter an English statement to translate:");
        translateMe = input.next();
        translateMe = translateMe.toLowerCase();
        System.out.println(" Translating:  [" + translateMe + "]");

        System.out.println("Translating: " + translateMe);
        String[] englishChar = translateMe.split(" ");

        for(String s : englishChar) {
            System.out.println("Translation String: " + s);
        }

        for ( int x = 0; x < englishChar.length; x++)
        {
            System.out.println("Checking word: " + englishChar[x]);
            for (int index = 0; index < english.length; index++)
            {
                System.out.println("Comparing equality of: " + englishChar[x] + " and " + english[index] + " they are " + (englishChar [ x ].equals( english[index]) ? " equal" :  " not equal"));
                if( englishChar [ x ].equals( english[index]))
                {
                    System.out.print(morse[index] + " "); // Display Morse Code array character at matching index position to show translation
                }

            }
        }

    }

}

}

于 2016-02-26T02:48:13.767 に答える
0

得られる出力を理解できません。実行してみたことはありませんが、目で見てみると、非常に異なる結果が得られます。

ただし、基本的には、モールス信号から英語への翻訳者のマッチングが早すぎます。文字列全体を確認する必要があります。

編集: 明らかなことは何もありません。実際に何が起こっているかを確認するために、デバッガーでステップスルーしてみましたか?

于 2016-02-26T02:19:20.377 に答える