gpt4 book ai didi

java - Java 中带有 HashMap 的摩尔斯电码翻译器

转载 作者:行者123 更新时间:2023-12-01 12:40:04 25 4
gpt4 key购买 nike

我确实意识到这个问题已经被问过,并且我已经在他们中寻找帮助。我正在尝试编写一个摩尔斯电码翻译器。可以翻译字母和数字;我已经使用 HashMap 使英语到莫尔斯电码完美地工作。 Google 之神告诉我,我可以颠倒 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++;
}
}
}

}

最佳答案

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

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 中带有 HashMap 的摩尔斯电码翻译器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25196198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com