将莫尔斯电码翻译成字母 [英] Translate morse code into alphabets

查看:206
本文介绍了将莫尔斯电码翻译成字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on a project that could translate morse code into english and vice versa. The below is the specific instruction: "Your program shall prompt the user to specify the desired type of translation, input a string of Morse Code characters or English characters, then display the translated results. When inputting Morse Code, separate each letter/digit with a single space, and delimit multiple words with a "|". For example, - --- | -... . would be the Morse Code input for the sentence "to be". Your program only needs to handle a single sentence and can ignore punctuation symbols."

尽管我想出了如何将英语翻译成莫尔斯电码,但是我却不知道如何将莫尔斯电码翻译成英语.如果您正在阅读本文,请帮助我!任何帮助或提示将不胜感激!谢谢:)

Although I figured out how to translate english to morse code, I cannot figure out how to translate morse code to english. And if you are reading this please help me! Any help or hints would be very much appreciated! Thanks :)

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) {

    }
}

推荐答案

我建议使用Hashtable创建字典,在该字典中,字母可以用作键,而相关的摩尔斯电码可以与此键配对.如果您希望拥有唯一的键值对,则可以使用 BiMap 用于存储.

I suggest to create a dictionary using Hashtable, where Alphabets can be used as Key and relevant Morse Code can be paired with this key. If you want to have unique Key Value pair you can use BiMap for storage.

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

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

you can easily access this map to get either keys or values

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

这篇关于将莫尔斯电码翻译成字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆