我正在尝试编写将英语翻译成摩尔斯电码的东西,反之亦然。我让英语到莫尔斯语运行良好,但我很难以另一种方式转换它。我的意思是|
用作单词之间的空格,所以- --- | -... .
意思是“成为”。谁能告诉我我的代码部分有什么问题case:1
?
public class Project {
public static void main(String[] args)
{
String[] morse = { ".- ", "-... ", "-.-. ", "-.. ", ". ", "..-. ", "--. ", ".... ", ".. ",
".--- ", "-.- ", ".-.. ", "-- ", "-. ", "--- ", ".--. ", "--.- ", ".-. ", "... ", "- ", "..- ",
"...- ", ".-- ", "-..- ", "-.-- ", "--.. ", "|" };
String[] alphabet = { "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", " " };
int a;
a = Input.getInt("Enter 1 for Morse to English or enter 2 for English to Morse.");
switch (a)
{
case 2:
{
String toMorse = new String();
toMorse = Input.getString("Enter a sentence.");
toMorse = toMorse.toUpperCase();
for (int i = 0; i < toMorse.length(); i++)
{
char x = toMorse.charAt(i);
if (x == ' ') System.out.print(" | ");
else System.out.print(morse[x - 'A']);
}
break;
}
case 1:
{
String toEnglish = new String();
toEnglish = Input.getString("Enter your Morse code.");
String[] parts = toEnglish.split("|");
for (int x = 0; x < parts.length; x++)
{
if (parts.equals(morse[x])) System.out.println(alphabet[x]);
}
break;
}
default:
{
System.out.println("Invalid Response");
}
}
}
}