Java 中的文本到莫尔斯翻译器

Text to morse translator in Java

我正在尝试在 java 中写莫尔斯码 decoder/encrypter。我有一个问题,某处出错了(在函数 fromMorse 中,它是从文本到莫尔斯的解码器)。

有没有人知道我做错了什么?

package mor;
 import java.util.Scanner;

public class Mor {
public static void main(String[] args) 
{

    Scanner input = new Scanner(System.in); 
    System.out.println("Type 1 for text to morse, 0 from morse to text."); 
    String myChoice = input.nextLine(); 
    System.out.println("Type what you want to decode/encode ");
    String userMessage = input.nextLine();
    System.out.println("");

if("1".equals(myChoice)){
 System.out.println(toMorse(userMessage));}
else{
     System.out.println(fromMorse(userMessage));
}

}

public static String fromMorse(String userMessage)
{
   String s = userMessage;
        System.out.printf("Message: %s\n", s);
        // string with decoded message
        String message = "";

        // sample alphabet string
        String alphabet = "abcdefghijklmnopqrstuvwxyz";
        String[] morseAlphabet = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....",  
        "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", 
        "...-", ".--", "-..-", "-.--", "--.."};

        // separate on string letters
        String[] letters = s.split(" ");

        // iteration morse letters
        for (String morseAlph : letters)
        {
            char alphabetLetter = '?';

            int index = -1;
            for (int i = 0; i < morseAlphabet.length; i++)
            {
                if (morseAlphabet[i].equals(morseAlph))
                    index = i;
            }

            if (index >= 0) // letter found
                alphabetLetter = alphabet.charAt(index);
            message += alphabetLetter;
        }

    return message;




}

public static String toMorse(String userMessage)
{
    String s = userMessage;
        System.out.printf("Message: %s\n", s);

        String message = "";



        String alphabet = "abcdefghijklmnopqrstuvwxyz";
        String[] morseAlphabet = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....",  
        "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", 
        "...-", ".--", "-..-", "-.--", "--.."};


        for (char alphabetLetter : message.toCharArray())
        {
           int index = -1;
           String letter = " ";
            for (int i = 0; i < alphabet.length(); i++)
                {
                    if (alphabet.charAt(i) == alphabetLetter)
                        {
                        index = i;
                        }
                }
            if (index >=0)
                {
                    letter = morseAlphabet[index];
                }
            message += letter + " ";
        }
       System.out.printf("Message: %s\n", message);

    return message;


}
}

toMorse() 方法在这里包含一个简单的错误:

for (char alphabetLetter : message.toCharArray())

您正在迭代 message 而不是 userMessage,因此它应该是:

for (char alphabetLetter : userMessage.toCharArray())