首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将摩尔斯电码翻译成字母表

将摩尔斯电码翻译成字母表
EN

Stack Overflow用户
提问于 2015-01-19 13:52:11
回答 3查看 1.3K关注 0票数 0

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

虽然我知道如何将英语翻译成摩尔斯电码,但我不知道如何将摩尔斯电码翻译成英语。如果你正在读这篇文章,请帮帮我!如有任何帮助或提示,将不胜感激!谢谢:)

代码语言:javascript
运行
复制
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) {

    }
}
EN

回答 3

Stack Overflow用户

发布于 2015-01-19 14:21:21

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

代码语言:javascript
运行
复制
Map<String, String> codeMap = new HashMap<String, String>();
codeMap.put("A", ".- ");
codeMap.put("B", "-... ");

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

代码语言:javascript
运行
复制
for (String key: codeMap.keySet() ){
    System.out.println(key+" : "+codeMap.get(key) );
}
票数 1
EN

Stack Overflow用户

发布于 2015-01-19 14:20:44

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

代码语言:javascript
运行
复制
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);
}
票数 0
EN

Stack Overflow用户

发布于 2015-01-19 14:34:43

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

代码语言:javascript
运行
复制
HashMap<String,String> map=new HashMap<String,String>();
//populate map
map.put(".- ","a");
map.put("-... ","b");

然后在EngToMo(String string1)方法中

代码语言:javascript
运行
复制
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)方法中

代码语言:javascript
运行
复制
public static void MoToEng(String string2){
 System.out.println(map.get(string2));
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28018666

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档