我对 Java 还是很陌生,一直在尝试让莫尔斯电码翻译器工作。我通过各种错误克服了第一个问题,但现在程序编译但不会打印翻译结果。任何帮助,将不胜感激。
import java.util.Scanner;
public class MorseCode
{
public static void main(String[] args)
{
Scanner Input = new Scanner(System.in);
System.out.println("To convert English to Morse Code, type M. To convert Morse Code to English, type E.");
String cType = Input.nextLine();
String type = cType.toLowerCase();
if(type == "m")
{
String eng;
System.out.println("Please enter the English text to be translated.");
eng = Input.nextLine();
EToM(eng);
}
else
{
String morse;
System.out.println("Please enter the Morse code text to be translated, with multiple words seperated by a |.");
morse = Input.nextLine();
MToE(morse);
}
}
public static void EToM(String eng)
{
String EToMList[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".--", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "----.", "-----", "|"};
String alphabet = "abcdefghijklmnopqrstuvwxyz123456789 ";
String translation[] = new String[eng.length()];
for(int x = 0; x < eng.length(); x++)
{
for(int y = 0; y < alphabet.length(); y++)
{
if(eng.charAt(x) == alphabet.charAt(y))
{
translation[x] = EToMList[y];
System.out.println("test");
}
}
}
System.out.println("Your translated message is:");
for(int z = 0; z < eng.length(); z++)
{
System.out.println(translation[z]);
}
}
public static void MToE(String morse)
{
}
}