【问题标题】:Why is my Java Morse Code translator giving me the wrong output?为什么我的 Java 摩尔斯电码翻译器给了我错误的输出?
【发布时间】:2016-02-26 01:53:53
【问题描述】:

我已经看到很多关于摩尔斯电码翻译器的问题,并查看了其中的许多问题,但在所有问题中,建议的答案都给了我相同的错误输出。代码背后的想法是能够使用数组将摩尔斯电码翻译成英语,反之亦然。我的代码如下:

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

作为输出。我检查了我的两个字符串数组以确保morse[A]english[A] 位于相同的索引位置等等,这些都很好。我想不出还有什么会导致这个问题的。

编辑:知道我正在使用 IntelliJ IDEA 15 可能会有所帮助

【问题讨论】:

    标签: java morse-code


    【解决方案1】:

    由于您使用 Scanner,无法将莫尔斯语翻译成英语。你需要使用 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
    

    【讨论】:

    • 这非常适合英语到莫尔斯语,但是莫尔斯语到英语我错过了'b'
    • 对不起,杰斯,你错过了“b”是什么意思?
    • 啊,对不起,我的“to be”测试(从莫尔斯到英语)中的“b”不见了。我一直用它作为测试短语。
    • 啊...好吧,让我试着来回映射“to be”,看看我得到了什么。
    • 如果有帮助,所有其他输入似乎都可以正常工作。甚至是单独的 'b' 和其他包含单词的 'b'。
    【解决方案2】:
    1. n ("-.") 的代码在您的数组中出现两次。
    2. 最好将字符串转换为char[]。您必须将字母表从 String[] 更改为 char[]
    3. 这就是为什么我们有Map

    【讨论】:

    • 谢谢!我想我是因为盯着它看了这么久而错过了它。我现在得到'to'的莫尔斯输出和英语输出't',所以我的翻译器停止得太早了。有什么想法吗?
    • 见@Ian 的评论。这将解释缺少的输出。
    【解决方案3】:

    我无法理解你得到的输出——虽然我没有尝试运行它,但通过目测我看到了一个非常不同的结果。

    不过,从根本上说,您的莫尔斯-英语翻译器匹配得太早了。您需要检查整个字符串。

    编辑:现在没有什么明显的问题,您是否尝试过使用调试器单步执行它以查看实际发生的情况?

    【讨论】:

    • 抱歉,我已经发布了我的代码的早期版本。我已将原始代码编辑为我目前的代码。这个版本(我认为)贯穿整个用户输入。但是,两个版本都给了我相同的错误输出。
    • 调试器给了我 '- --- 与目标 VM 断开连接,地址:'127.0.0.1:61223',传​​输:'socket'' 但老实说我不知道​​这意味着什么。跨度>
    【解决方案4】:

    你从英文翻译中得到“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
                    }
    
                }
            }
    
        }
    
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-11
      • 2010-12-23
      • 2023-03-31
      • 2020-07-02
      • 1970-01-01
      相关资源
      最近更新 更多