0

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

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

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

    }
}
4

3 回答 3

1

我建议使用 Hashtable 创建一个字典,其中 Alphabets 可以用作 Key,并且可以将相关的摩尔斯电码与此键配对。如果您想拥有唯一的键值对,您可以使用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) );
}
于 2015-01-19T06:21:21.447 回答
0

为什么不使用 aMap<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));
}
于 2015-01-19T06:34:43.107 回答
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);
}
于 2015-01-19T06:20:44.247 回答