我想构建一个将英语翻译成摩尔斯电码的程序,反之亦然,我决定使用哈希映射来做到这一点,但我不确定如何通过哈希映射运行字符串并在结束。这是我目前的代码:
import java.util.HashMap;
import java.util.Map;
public class MorseCodeTranslator{
public static String translateToMorseCode() {
String englishtoMorse = "";
String translation = null;
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', "__..");
return translation;
}
public static String translateFromMorseCode() {
String morsetoEnglish = "";
String translation = null;
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', "__..");
return translation;
}
}
我希望能够运行哈希映射中englishtoMorse
或morsetoEnglish
通过哈希映射的任何内容,并将字符转换为与哈希映射关联的值,然后将它们输出到translation
.