快速需要Java Morse代码转换器的帮助 [英] Need Assistance with Java Morse Code Translator Quickly

查看:89
本文介绍了快速需要Java Morse代码转换器的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的Java入门课程,我应该创建一个Morse代码转换器,该转换器可以将英语转换为Morse,也可以将Morse转换为英语.我的代码可以将英语转换为莫尔斯电码,但似乎无法设法从莫尔斯电码转换为英语.这是提示:

For my Intro to Java class I'm supposed to create a Morse code translator that can convert both from English to Morse and from Morse to English. My code works for converting English to Morse, but can't seem to manage to go the other way, from Morse Code to English. Here is the prompt:

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

This project involves writing a program to translate Morse Code into English and English into Morse Code. Your program shall prompt the user to specify the desired type of translation, input a string of Morse Code characters or English characters, then display the translated results. When inputting Morse Code, separate each letter/digit with a single space, and delimit multiple words with a "|". For example, - --- | -… . would be the Morse Code input for the sentence "to be". Your program only needs to handle a single sentence and can ignore punctuation symbols. When inputting English, separate each word with a blank space.

我想知道我当前的摩尔斯电码"代码有什么问题.如果您了解出了什么问题,请提供帮助,我已经花费了数小时试图弄清楚这件事,而且我必须在2015年8月26日太平洋时间午夜之前解决此问题.谢谢! 这是我的代码:

I am wondering what's wrong with my current "Morse code" code. Please help if you understand what's going wrong, I've spent hours trying to figure this thing out and I've got to this before midnight Pacific Time on 8/26/2015. Thanks! Here is my code:

//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" );

            }

        }



}

推荐答案

在这里,此代码将为您效劳,您也使英语莫尔斯电码错误,我也对此进行了修正

here this code will work for you, you had your english to morse code wrong too i fixed that too

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" );

                }

            }

这篇关于快速需要Java Morse代码转换器的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆