-6

对于我的 Java 简介类,我应该创建一个莫尔斯电码翻译器,它可以将英语转换为莫尔斯电码,也可以将莫尔斯电码转换为英语。我的代码适用于将英语转换为莫尔斯电码,但似乎无法从莫尔斯电码转换为英语。这是提示:

该项目涉及编写一个程序,将摩尔斯电码翻译成英语和将英语翻译成摩尔斯电码。您的程序应提示用户指定所需的翻译类型,输入一串摩尔斯电码字符或英文字符,然后显示翻译结果。输入摩尔斯电码时,每个字母/数字用一个空格分隔,多个单词用“|”分隔。例如 - --- | -…… 将是句子“to be”的摩尔斯电码输入。你的程序只需要处理一个句子,可以忽略标点符号。输入英文时,每个单词用空格隔开。

我想知道我当前的“莫尔斯电码”代码有什么问题。如果您了解出了什么问题,请提供帮助,我已经花了几个小时试图解决这个问题,我已经在太平洋时间 2015 年 8 月 26 日午夜之前解决了这个问题。谢谢!这是我的代码:

//Justin Buckley
//8.26.2015
import java.util.Scanner;
public class MorseCodeProject1 {

    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 = { ".-" , "-..." , "-.-." , "-.." , "." , "..-." , "--." , "...." , ".." , ".---" , "-.-" , ".-.." , "--" , "-." , "---" , ".--." , "--.-" ,  ".-." , "..." , "-" , "..-" , "...-" , ".--" , "-..-" , "-.--" , "--.." , "|" };

        Scanner input = new Scanner (System.in);
        System.out.println( "Please enter \"MC\" if you want to translate Morse Code into English, or \"Eng\" if you want to translate from English into Morse Code" );
        String a = input.nextLine();
            if ( a.equalsIgnoreCase("MC"))
            {
                System.out.println( "Please enter a sentence in Morse Code. Separate each letter/digit with a single space and delimit multiple words with a | ." );
                String b = input.nextLine();

                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 < Morse.length; m++)
                        {
                            if (character.equals(Morse[m]))   
                                System.out.print(English[ m ]);    
                        }    
                    }
                    System.out.print(" ");    
                }    
            }

            else if ( a.contains("Eng" ))
            {
                System.out.println("Please enter a sentence in English, and separate each word with a blank space.");
                String c = input.nextLine(); 

                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

2 回答 2

2

在这里,这段代码对你有用,你的英语到莫尔斯电码也错了,我也修复了

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 = { ".-" , "-..." , "-.-." , "-.." , "." , "..-." , "--." , "...." , ".." , ".---" , "-.-" , ".-.." , "--" , "-." , "---" , ".--." , "--.-" ,  ".-." , "..." , "-" , "..-" , "...-" , ".--" , "-..-" , "-.--" , "--.." , "|" };

            Scanner input = new Scanner (System.in);
            System.out.println( "Please enter \"MC\" if you want to translate Morse Code into English, or \"Eng\" if you want to translate from English into Morse Code" );
            String a = input.nextLine();
                if ( a.equalsIgnoreCase("MC"))
                {
                    System.out.println( "Please enter a sentence in Morse Code. Separate each letter/digit with a single space and delimit multiple words with a | ." );
                    String b = input.nextLine();
                    String[] words = null;
                    if(b.contains("|")){
                     words = b.split("[|]");
                    }else{
                        words = new String[1];
                        words[0] = b;
                    }

                    for (String word: words )
                    {
                        String[] characters = word.split(" ");
                        for(int h = 0;h < characters.length;h++){
                        for(int i = 0;i < Morse.length;i++){
                            if(characters[h].equals(Morse[i])){
                                System.out.print(English[i]);
                            }
                        }
                        }
                        System.out.print(" ");    
                    }    
                }

                else if ( a.contains("Eng" ))
                {
                    System.out.println("Please enter a sentence in English, and separate each word with a blank space.");
                    String c = input.nextLine(); 

                    c = c.toLowerCase ();

                    for ( int x = 0; x < c.length(); x++ )
                    {
                        for ( int y = 0; y < English.length; y++ )
                        {
                            if ( English [ y ] == c.charAt ( x ) )

                            System.out.print ( Morse [ y ] + " " );


                        }

                    }


                }

                else 
                {
                    System.out.println ( "Invalid Input" );

                }

            }
于 2015-08-27T04:02:22.133 回答
0

继我的评论之后:在问这种问题之前,你真的需要做功课。

所有答案都可以在 Java 教程中找到:https ://docs.oracle.com/javase/tutorial/java/data/strings.html

更具体地说,您想知道如何操作字符串:https ://docs.oracle.com/javase/tutorial/java/data/manipstrings.html

与这里的教程保持一致的是一个基本示例,展示了如何实现您想要的:

if ( a.equalsIgnoreCase("MC"))
{
    System.out.println( "Please enter a sentence in Morse Code. Separate each letter/digit with a single space and delimit multiple words with a | ." );
    //Example input string
    String b = ".... . .-.. .-.. --- | .-- --- .-. .-.. -.. | .. -. | -- --- .-. ... ." ;

    //We cant use the split method here because it removes blank spaces, so lets keep it basic:
    //String[] words = b.split("|");
    int letterStart = 0;
    int letterEnd = 0;
    String letter = "";
    while (letterStart < b.length())
    {
        letterEnd = b.indexOf(" ", letterStart);
        //check if end of string has been reached 
        if (letterEnd == -1)
        {
          //grab last morse letter/sequence
          letter = b.substring(letterStart);
        }
        else
        {
          //grab next morse letter/sequence
          letter = b.substring(letterStart, letterEnd);
        }

        //convert each morse sequence to english 
        for (int m = 0; m < Morse.length; m++)
        {
        if (letter.equals("|"))
          {
            //show spaces between words
            System.out.print(" ");
            break;
          }
          else if (letter.equals(Morse[m]))
          {
            //show english letter
            System.out.print(English[m]);
            break;
          }
        }
        if (letterEnd == -1)
        {
        break;
        }
        letterStart = letterEnd+1;
    }
}
于 2015-08-27T04:19:53.787 回答