2

我正在尝试用 java 编写莫尔斯解码器/加密器。我有一个问题,某处是错误的(在函数 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;


}
}
4

1 回答 1

4

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

for (char alphabetLetter : message.toCharArray())

您正在迭代message而不是userMessage,所以它应该是:

for (char alphabetLetter : userMessage.toCharArray())

于 2016-01-26T15:50:34.187 回答