我刚刚制作了一个 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" );
}
}
}