我现在的问题是让“莫尔斯电码到英语”工作。将英语转换为莫尔斯语的第一部分运行良好。我知道以前有人问过这个问题,但我不知道我做错了什么。我知道我需要在某个地方进行拆分,但我只是不确定将其放在代码中的哪个位置。现在莫尔斯到英文部分甚至不会转换一个符号。任何帮助都感激不尽。
import java.util.Scanner;
public class MorseCode2 {
public static void main ( String [] args )
{
Scanner input = new Scanner( System.in );
System.out.print( "Would you like to convert English to Morse (yes or no)? " );
String answer = input.nextLine();
if( answer.equals( "yes" ) )
{
System.out.println( "Please enter the text you want to translate into Morse code: " );
String english = input.nextLine();
System.out.println( stringToMorse( english ) );
}
if (answer.equalsIgnoreCase( "no" ) )
{
System.out.print( "Morse to English? " );
String answer2 = input.nextLine();
if (answer2.equalsIgnoreCase( "yes" ) )
{
System.out.println( "Please enter the Morse code you want to translate into English: " );
String code = input.nextLine();
System.out.println( stringToEnglish( code ) );
}
}
}
public static String encode (String toEncode)
{
String morse = toEncode;
if (toEncode.equalsIgnoreCase("a"))
morse = ".-";
if (toEncode.equalsIgnoreCase("b"))
morse = "-...";
if (toEncode.equalsIgnoreCase("c"))
morse = "-.-.";
if (toEncode.equalsIgnoreCase("d"))
morse = "-..";
if (toEncode.equalsIgnoreCase("e"))
morse = ".";
if (toEncode.equalsIgnoreCase("f"))
morse = "..-.";
if (toEncode.equalsIgnoreCase("g"))
morse = "--.";
if (toEncode.equalsIgnoreCase("h"))
morse = "....";
if (toEncode.equalsIgnoreCase("i"))
morse = "..";
if (toEncode.equalsIgnoreCase("j"))
morse = ".---";
if (toEncode.equalsIgnoreCase("k"))
morse = "-.-";
if (toEncode.equalsIgnoreCase("l"))
morse = ".-..";
if (toEncode.equalsIgnoreCase("m"))
morse = "--";
if (toEncode.equalsIgnoreCase("n"))
morse = "-.";
if (toEncode.equalsIgnoreCase("o"))
morse = "---";
if (toEncode.equalsIgnoreCase("p"))
morse = ".--.";
if (toEncode.equalsIgnoreCase("q"))
morse = "--.-";
if (toEncode.equalsIgnoreCase("r"))
morse = ".-.";
if (toEncode.equalsIgnoreCase("s"))
morse = "...";
if (toEncode.equalsIgnoreCase("t"))
morse = "-";
if (toEncode.equalsIgnoreCase("u"))
morse = "..-";
if (toEncode.equalsIgnoreCase("v"))
morse = "...-";
if (toEncode.equalsIgnoreCase("w"))
morse = ".--";
if (toEncode.equalsIgnoreCase("x"))
morse = "-..-";
if (toEncode.equalsIgnoreCase("y"))
morse = "-.--";
if (toEncode.equalsIgnoreCase("z"))
morse = "--..";
if (toEncode.equalsIgnoreCase("0"))
morse = "-----";
if (toEncode.equalsIgnoreCase("1"))
morse = ".----";
if (toEncode.equalsIgnoreCase("2"))
morse = "..---";
if (toEncode.equalsIgnoreCase("3"))
morse = "...--";
if (toEncode.equalsIgnoreCase("4"))
morse = "....-";
if (toEncode.equalsIgnoreCase("5"))
morse = ".....";
if (toEncode.equalsIgnoreCase("6"))
morse = "-....";
if (toEncode.equalsIgnoreCase("7"))
morse = "--...";
if (toEncode.equalsIgnoreCase("8"))
morse = "---..";
if (toEncode.equalsIgnoreCase("9"))
morse = "----.";
if (toEncode.equalsIgnoreCase("."))
morse = ".-.-";
if (toEncode.equalsIgnoreCase(","))
morse = "--..--";
if (toEncode.equalsIgnoreCase("?"))
morse = "..--..";
return morse;
}
public static String decode (String toEncode) {
String morse = toEncode;
if (toEncode.equalsIgnoreCase(".-"))
morse = "a";
if (toEncode.equalsIgnoreCase("-..."))
morse = "b";
if (toEncode.equalsIgnoreCase("-.-."))
morse = "c";
if (toEncode.equalsIgnoreCase("-.."))
morse = "d";
if (toEncode.equalsIgnoreCase("."))
morse = "e";
if (toEncode.equalsIgnoreCase("..-."))
morse = "f";
if (toEncode.equalsIgnoreCase("--."))
morse = "g";
if (toEncode.equalsIgnoreCase("...."))
morse = "h";
if (toEncode.equalsIgnoreCase(".."))
morse = "i";
if (toEncode.equalsIgnoreCase(".---"))
morse = "j";
if (toEncode.equalsIgnoreCase("-.-"))
morse = "k";
if (toEncode.equalsIgnoreCase(".-.."))
morse = "l";
if (toEncode.equalsIgnoreCase("--"))
morse = "m";
if (toEncode.equalsIgnoreCase("-."))
morse = "n";
if (toEncode.equalsIgnoreCase("---"))
morse = "o";
if (toEncode.equalsIgnoreCase(".--."))
morse = "p";
if (toEncode.equalsIgnoreCase("--.-"))
morse = "q";
if (toEncode.equalsIgnoreCase(".-."))
morse = "r";
if (toEncode.equalsIgnoreCase("..."))
morse = "s";
if (toEncode.equalsIgnoreCase("-"))
morse = "t";
if (toEncode.equalsIgnoreCase("..-"))
morse = "u";
if (toEncode.equalsIgnoreCase("...-"))
morse = "v";
if (toEncode.equalsIgnoreCase(".--"))
morse = "w";
if (toEncode.equalsIgnoreCase("-..-"))
morse = "x";
if (toEncode.equalsIgnoreCase("-.--"))
morse = "y";
if (toEncode.equalsIgnoreCase("--.."))
morse = "z";
if (toEncode.equalsIgnoreCase("-----"))
morse = "0";
if (toEncode.equalsIgnoreCase(".----"))
morse = "1";
if (toEncode.equalsIgnoreCase("..---"))
morse = "2";
if (toEncode.equalsIgnoreCase("...--"))
morse = "3";
if (toEncode.equalsIgnoreCase("....-"))
morse = "4";
if (toEncode.equalsIgnoreCase("....."))
morse = "5";
if (toEncode.equalsIgnoreCase("-...."))
morse = "6";
if (toEncode.equalsIgnoreCase("--..."))
morse = "7";
if (toEncode.equalsIgnoreCase("---.."))
morse = "8";
if (toEncode.equalsIgnoreCase("----."))
morse = "9";
if (toEncode.equalsIgnoreCase("|"))
morse = "";
return morse;
}
public static String stringToMorse( String text )
{
String newText = "";
String selectedChar;
String convertedChar;
for (int i = 0; i < text.length(); i++)
{
//Select the next character
selectedChar = text.charAt(i) + "";
// Convert the character
convertedChar = encode(selectedChar);
if (convertedChar.equals(" ")) // " " separates each word represented in english code
{
newText = newText + "| ";
}
// Add the converted text, and add a space
else
{
newText = newText + convertedChar;
if (!convertedChar.equals(" "))
{
newText = newText + " ";
}
}
}
return newText;
}
public static String stringToEnglish( String text )
{
String newMorse = ""; //sets string for newMorse
String selectedMorse; //sets string for selectedMorse
String convertedMorse; //sets string for convertedMorse
for (int i = 0; i < text.length(); i++)
{
//Select the next character
selectedMorse = text.charAt(i) + "";
// Convert the character
convertedMorse = encode(selectedMorse);
if (convertedMorse.equals("| ")) // "|" separates each word represented in morse code
{
newMorse = newMorse + " ";
}
// Add the converted text, and add a space
else
{
newMorse = newMorse + convertedMorse;
if (!convertedMorse.equals(" "))
{
newMorse = newMorse + " ";
}
}
}
return newMorse;
}
}