我正在从事一个项目,可以将摩尔斯电码翻译成英语,反之亦然。以下是具体的说明:“你的程序将提示用户指定所需的翻译类型,输入摩尔斯电码字符串或英文字符,然后显示翻译结果。在输入摩尔斯电码时,用一个空格分隔每个字母/数字,并用”|“分隔多个单词。例如,-| -...将是句子”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) {
}
}
发布于 2015-01-19 14:21:21
我建议使用Hashtable创建一个字典,其中Alphabets可以用作键,相关的摩尔斯电码可以与此键配对。如果您想拥有唯一的键值对,您可以使用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-19 14:20:44
你可以拆分输入的字符串,然后解析它的内容...
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-19 14:34:43
为什么不使用Map<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));
}
https://stackoverflow.com/questions/28018666
复制相似问题