0

我刚刚制作了一个 java 莫尔斯电码翻译器,但是当我将莫尔斯电码转换为英语时,它会将每个符号转换为一个字母,所以当我翻译它时......它给了我 eeee 而不是 h。所以我的问题是如何做到这一点,以便我可以用摩尔斯电码翻译整个单词并使用 | 将单词分开。

这是我的代码

public class project1 {

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 = { ".-" , "-..." , "-.-." , "-.." , "." , "..-." , "--." , "...." , ".." , ".---" , "-.-" , ".-.." , "--" , "-." , "---" , ".--." , "--.-" ,  ".-." , "..." , "-" , "..-" , "...-" , ".--" , "-..-" , "-.--" , "--.." , "|" };
    String a = Input.getString ( "Please enter MC if you want to translate Morse Code into English, or Eng if you want to translate from English into Morse Code" );
if (a.equals("MC"))
    {
        String b = Input.getString ("Please enter a sentence in Morse Code. Separate each letter/digit with a single space and delimit multiple words with a | .");    

        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.equals("Eng"))
    {
        String c = Input.getString ( "Please enter a sentence in English, and separate each word with a blank space." );

        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

0

您遇到的一个问题是您的String#split(...)方法没有考虑到|正则表达式的特殊字符,并且没有像您期望的那样拆分字符串。您需要转义此字符才能正常工作。所以改变这个:

String[] words = b.split("|");

对此:

String[] words = b.split("\\|"); // escaping the pipe char

要调试并查看事情是否按预期工作,请使用调试器或在代码中添加 println,例如:

 String[] words = b.split("\\|");

 // TODO: remove line below from finished program
 System.out.println(java.util.Arrays.toString(words)); 

顺便说一句,如果这是我的程序,我会HashMap<String, String>帮助我将英语翻译成莫尔斯语,反之亦然。

在我刚刚执行的进一步搜索中,请查看:为什么 String.split 需要转义管道分隔符?有关为什么需要转义管道字符的背景信息。

于 2015-05-30T21:07:02.347 回答
-1

这是您改编的代码,可以正常工作...

import java.util.Scanner;

public class project1 {

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 scanner = 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 = scanner.nextLine();

if (a.equals("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 = scanner.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.equals("Eng"))
    {
        System.out.println("Please enter a sentence in English, and separate each word with a blank space.");
        String c = scanner.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" );

    }

}
}
于 2015-05-30T20:33:55.213 回答