我的确意识到有人提出了这个问题,并在他们中寻求帮助。我正在尝试编写摩尔斯电码翻译器。可以同时翻译字母和数字;我已经使英语成为摩尔斯人,并且可以使用HashMap完美地工作。谷歌众神告诉我,我可以颠倒哈希图的顺序,以便将莫尔斯电码翻译成英文,但是我正在空白。

我要寻找的是一种在莫尔斯电码中输入字符串(包括空格)并获取英文翻译(包括数字)的方法?

import java.util.*;

public class Project {
    public static void main(String[] args) {
        getInput();
    }

    public static void getInput() {
        char a;
        a = Input.getChar("Enter m for Morse to English or e for English to Morse.");
        if (a == 'm')
            translateMorse();
        else if (a == 'e')
            translateEnglish();
        else
            System.out.println("Your input is not valid.");
    }

    public static void translateEnglish() {
        String englishToMorse = Input.getString("Enter a phrase in English.");
        englishToMorse = englishToMorse.toLowerCase();

        int i = 0;

        while(i <englishToMorse.length()) {
            Map<Character, String> morse = new HashMap<Character, String>();
            morse.put('a', ".-");
            morse.put('b', "-...");
            morse.put('c',  "-.-");
            morse.put('d',  "-..");
            morse.put('e',    ".");
            morse.put('f', "..-.");
            morse.put('g',  "--.");
            morse.put('h', "....");
            morse.put('i',   "..");
            morse.put('j', ".---");
            morse.put('k',   "-.");
            morse.put('l', ".-..");
            morse.put('m',   "--");
            morse.put('n',   "-.");
            morse.put('o',  "---");
            morse.put('p', ".--.");
            morse.put('q', "--.-");
            morse.put('r', ".-.");
            morse.put('s',  "...");
            morse.put('t',   "-");
            morse.put('u',  "..-");
            morse.put('v', "...-");
            morse.put('w',  ".--");
            morse.put('x', "-..-");
            morse.put('y', "-.--");
            morse.put('z', "--..");
            morse.put('1', ".----");
            morse.put('2',"..---");
            morse.put('3', "...--");
            morse.put('4', "....-");
            morse.put('5', ".....");
            morse.put('6', "-....");
            morse.put('7', "--...");
            morse.put('8', "---..");
            morse.put('9', "----.");
            morse.put('0', "-----");
            System.out.print(morse.get(englishToMorse.charAt(i)));
            i++;
        }
    }

    public static void translateMorse() {
        String morseToEnglish = Input.getString("Enter a phrase in Morse Code.");
        morseToEnglish = morseToEnglish.toLowerCase();

        int i = 0;
        while(i < morseToEnglish.length() ) {
            HashMap<Character, String> morse = new HashMap<>();
            morse.put('a', ".-");
            morse.put('b', "-...");
            morse.put('c',  "-.-");
            morse.put('d',  "-..");
            morse.put('e',    ".");
            morse.put('f', "..-.");
            morse.put('g',  "--.");
            morse.put('h', "....");
            morse.put('i',   "..");
            morse.put('j', ".---");
            morse.put('k',   "-.");
            morse.put('l', ".-..");
            morse.put('m',   "--");
            morse.put('n',   "-.");
            morse.put('o',  "---");
            morse.put('p', ".--.");
            morse.put('q', "--.-");
            morse.put('r', ".-.");
            morse.put('s',  "...");
            morse.put('t',   "-");
            morse.put('u',  "..-");
            morse.put('v', "...-");
            morse.put('w',  ".--");
            morse.put('x', "-..-");
            morse.put('y', "-.--");
            morse.put('z', "--..");
            morse.put('1', ".----");
            morse.put('2',"..---");
            morse.put('3', "...--");
            morse.put('4', "....-");
            morse.put('5', ".....");
            morse.put('6', "-....");
            morse.put('7', "--...");
            morse.put('8', "---..");
            morse.put('9', "----.");
            morse.put('0', "-----");

            Collection<String>  values= morse.values();

            for (String string: values) {
                System.out.print(string);
            }
            i++;
        }
    }
}

}

最佳答案

您可以通过遍历HashMap来反转EntrySet,如下所示:

Map<String, Character> morseToEngMap = new HashMap<>();
// engToMorseMap is your original hashmap object named morse.
// morseToEngMap is reversed map
for(Map.Entry<Character, String> entry : engToMorseMap.entrySet()){
    morseToEngMap.put(entry.getValue(), entry.getKey());
}


另外,您可以利用Guava library的方法,该方法具有inverse()的功能。

BiMap<Character, String> engToMorseMap = HashBiMap.create();
// engToMorseMap is your original hashmap object named morse.
morse.put('a', ".-");
morse.put('b', "-...");

BiMap<String, Character> morseToEngMap = morse.inverse();
// morseToEngMap is reversed map.


希望这对您有所帮助。

关于java - 带有Java HashMaps的摩尔斯电码翻译器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25196198/

10-10 10:07