簡體   English   中英

為什么我的Java Morse代碼翻譯器給我錯誤的輸出?

[英]Why is my Java Morse Code translator giving me the wrong output?

我看到了許多有關摩爾斯電碼翻譯器的問題,並查看了許多問題,但是在所有這些問題中,建議的答案都給了我同樣的錯誤輸出。 該代碼的思想是能夠使用數組將摩爾斯電碼翻譯成英文,反之亦然。 我的代碼如下:

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

作為輸出。 我已經遍歷了我的兩個String數組,以確保morse[A]english[A]處於相同的索引位置,依此類推,這些都很好。 我想不出會導致此問題的任何其他方法。

編輯:知道我正在使用IntelliJ IDEA 15可能會有所幫助

  1. n"-." )的代碼在您的數組中出現兩次。
  2. 您最好將String轉換為char[] 您必須將字母從String[]更改為char[]
  3. 這就是為什么我們有Map

由於您使用Scanner,因此無法將莫爾斯(Morse)譯成英文。 您需要使用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();

在那之后,即使使用split命令,它似乎也可以正常翻譯。

輸出:

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

輸出2:

Please enter a Morse Code statement to translate. Separate letters with spaces and words with a ' | '
- --- | -... .
[-, ---, |, -..., .]
to be

我無法理解您獲得的輸出-雖然我還沒有嘗試運行它,但是通過目測,我看到了截然不同的結果。

但是,從根本上講,您的莫爾斯英語譯者匹配得太早了。 您需要檢查整個字符串。

編輯:現在沒有明顯的跡象,您是否嘗試過與調試器一起逐步檢查實際情況?

之所以從英語翻譯中得到“ s”和“ n”,是因為如果我們檢查單詞“ to”,則字母中到“ t”的前一個字母是“ s”,字母中到“ o” “是” n“,因此使我相信其中存在循環錯誤。 此外,您使用的是“ next()”而不是“ nextLine()”,因此您沒有在解析自己的想法。 我認為,如果您使用“ 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
                }

            }
        }

    }

}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM