我有一個Java翻譯器,它可以在我從英語翻譯成摩爾斯而不是從莫爾斯翻譯成英文時使用。 如果你能告訴我我需要做些什麼才能使它變得好極了。在我輸入摩爾斯電碼後,當我從莫爾斯語轉到英語時,它只是結束了程序,而不是給我翻譯。需要有關java摩斯密碼翻譯器的幫助
這是我的代碼。
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 < b.length(); m++)
{
if (character.equals("inputMorseCode[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");
}
}
}
添加更多細節並定義您正面臨的問題... –
嘗試在此行中更改拆分條件:String [] characters = word.split(「」); to:String [] characters = word.split(「」);但我寧願寫我自己的函數,這使得char數組比使用.split – itwasntme