将莫尔斯电码转换为字母表

时间:2015-01-19 05:52:11

标签: java morse-code

我正在开发一个可以将莫尔斯电码翻译成英语的项目,反之亦然。以下是具体说明:"您的程序应提示用户指定所需的翻译类型,输入一串莫尔斯电码字符或英文字符,然后显示翻译结果。 输入摩尔斯电码时,用单个空格分隔每个字母/数字,并用“|”分隔多个单词。例如, - --- | -...将是“将来”的摩尔斯电码输入。您的程序只需处理一个句子,可以忽略标点符号。"

虽然我想出了如何将英语翻译成莫尔斯语代码,但我无法弄清楚如何将莫尔斯语代码翻译成英语。如果你正在读这个,请帮助我!任何帮助或提示将非常感谢!谢谢:))

public static String[] morse = { ".- ", "-... ", "-.-. ", "-.. ", ". ",
        "..-. ", "--. ", ".... ", ".. ",

        ".--- ", "-.- ", ".-.. ", "-- ", "-. ", "--- ", ".--. ", "--.- ",
        ".-. ", "... ", "- ", "..- ",

        "...- ", ".-- ", "-..- ", "-.-- ", "--.. ", "|" };

public static 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", " " };

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Input '1' to translate English to Morse Code.");
    System.out.println("Input '2' to translate Morse Code to English.");
    int kind = in.nextInt();

    if (kind == 1) {
        System.out.println("Please insert an alphabet string");
        String Eng = in.next();
        EngToMo(Eng);
    }
    if (kind == 2) {
        System.out.println("Please insert a morse string");
        String Mor = in.next();
        MoToEng(Mor);
    }

}

public static void EngToMo(String string1) {
    String Upper1 = string1.toUpperCase();
    for (int i = 0; i < Upper1.length(); i++) {
        char x = Upper1.charAt(i);
        if (x != ' ') {
            System.out.print(morse[x - 'A'] + " ");
        } else {
            System.out.println(" | ");
        }
    }
}

public static void MoToEng(String string2) {

    }
}

3 个答案:

答案 0 :(得分:1)

我建议使用Hashtable创建一个字典,其中Alphabets可以用作Key,相关的Morse Code可以与此密钥配对。如果您想拥有唯一的键值对,可以使用BiMap进行存储。

Map<String, String> codeMap = new HashMap<String, String>();
codeMap.put("A", ".- ");
codeMap.put("B", "-... ");

您可以轻松访问此地图以获取键或值

for (String key: codeMap.keySet() ){
    System.out.println(key+" : "+codeMap.get(key) );
}

答案 1 :(得分:0)

你可以拆分输入字符串,然后解析它的内容......

public static void MoToEng(String string2) {
    String[] splits = string2.split(" "); //you split the string into
                                           //String[] - delimiter = ' '


    String literal = ""; //this will be your result

    for (String split: splits){ //iterate through the String[] split

        splits = splits + " "; //your morse code ends with
                               //a blank - you have to add that 
                               //else equals() won't find 
                               //a suitable match
        //then look into your morse code
        for (int index = 0; index < morse.length(); index++){ 
            string code = morse[index];

            //if split looks like a morse code, you found your 
            //character
            if (split.equals(code){

                //translate back then
                literal = literal + ('A'+index);

            }
        }
    }
    System.out.println(literal);
}

答案 2 :(得分:0)

为什么不使用Map<String,String>来存储<morsecode,english alphabet>值?

HashMap<String,String> map=new HashMap<String,String>();
//populate map
map.put(".- ","a");
map.put("-... ","b");

然后在方法EngToMo(String string1)

public static void EngToMo(String string1){

for (Entry<String, String> entry : map.entrySet()) {
            if (entry.getValue().equals(string1)) {
                System.out.println(entry.getKey());
            }
        }
}

并在方法MoToEng(String string2)

public static void MoToEng(String string2){
 System.out.println(map.get(string2));
}