无法将摩尔斯语翻译成英语
Unable to translate morse to english
我有一个学校项目,我需要制作莫尔斯英语翻译器。我是 Java 方面的新手,所以我想知道您是否可以帮助我。问题是我不确定我应该在 while 循环中放置什么以将莫尔斯电码转换为英语。任何帮助,将不胜感激。谢谢^^
public static void main(String[]args){
HashMap<Character,String> translations=new HashMap<Character,String>();
translations.put('A', ".-");
translations.put('B', "-...");
translations.put('C', "-.-.");
translations.put('D', "-..");
translations.put('E', ".");
translations.put('F', "..-.");
translations.put('G', "--.");
translations.put('H', "....");
translations.put('I', "..");
translations.put('J', ".---");
translations.put('K', "-.-");
translations.put('L', ".-..");
translations.put('M', "--");
translations.put('N', "-.");
translations.put('O', "---");
translations.put('P', ".--.");
translations.put('Q', "--.-");
translations.put('R', ".-.");
translations.put('S', "...");
translations.put('T', "-");
translations.put('U', "..-");
translations.put('V', "...-");
translations.put('W', ".--");
translations.put('X', "-..-");
translations.put('Y', "-.--");
translations.put('Z', "--..");
translations.put('0', "-----");
translations.put('1', ".----");
translations.put('2', "..---");
translations.put('3', "...--");
translations.put('4', "....-");
translations.put('5', ".....");
translations.put('6', "-....");
translations.put('7', "--...");
translations.put('8', "---..");
translations.put('9', "----.");
translations.put(' ', " ");
HashMap<String, Character> reversedHashMap = new HashMap<String, Character>();
for (char key : translations.keySet()){
reversedHashMap.put(translations.get(key), key);
}Scanner scan=new Scanner(System.in);
System.out.println("Welcome to the translator. Type 1 for English to Morse or type 2 for Morse to English: ");
int choice=scan.nextInt();
if(choice==1)
engToMorse(translations);
else if(choice==2)
morseToEng(reversedHashMap);
else{
System.out.println("Invalid Input!");
}
}
public static void engToMorse(HashMap<Character,String> translations){
Scanner scan=new Scanner(System.in);
System.out.println("Please enter the sentence that you want to translate to Morse here: ");
String sentence=scan.nextLine().toUpperCase();
int i=0;
while(i<sentence.length()){
System.out.printf(translations.get(sentence.charAt(i)));
i++;
}
}
public static void morseToEng(HashMap<String,Character> reversedHashMap){
Scanner scan=new Scanner(System.in);
System.out.println("Please enter the morse code that you want to translate to english: ");
String morse=scan.nextLine().toUpperCase();
int i=0;
while(i<morse.length()){
System.out.print();
i++;
}
}
读取输入后,将输入字符串拆分 space。对于每个元素,查找摩尔斯电码(键)到字符(值)的映射并打印值。
for (String singleCode : morse.split(" ")) {
System.out.print(reversedHashMap.get(singleCode));
}
示例输入输出
Welcome to the translator. Type 1 for English to Morse or type 2 for Morse to English:
2
Please enter the morse code that you want to translate to english:
... -- ...
SMS
你可以简单地一个一个地获取莫尔斯电码,每行获取一个莫尔斯电码,处理它并把它连接成结果字符串,如果你想在一行中获取多个莫尔斯电码,在它们之间使用分隔符然后根据该拆分器拆分字符串,例如 #.
所以你会得到--#.--#..--
然后将其拆分为:
for (String singleMorse : morse.split("#")) {
System.out.print(reversedHashMap.get(singleCode));
}
注意:因为你的摩尔斯电码中有SPACE,使用[=17以外的分离器更简单=]SPACE
我有一个学校项目,我需要制作莫尔斯英语翻译器。我是 Java 方面的新手,所以我想知道您是否可以帮助我。问题是我不确定我应该在 while 循环中放置什么以将莫尔斯电码转换为英语。任何帮助,将不胜感激。谢谢^^
public static void main(String[]args){
HashMap<Character,String> translations=new HashMap<Character,String>();
translations.put('A', ".-");
translations.put('B', "-...");
translations.put('C', "-.-.");
translations.put('D', "-..");
translations.put('E', ".");
translations.put('F', "..-.");
translations.put('G', "--.");
translations.put('H', "....");
translations.put('I', "..");
translations.put('J', ".---");
translations.put('K', "-.-");
translations.put('L', ".-..");
translations.put('M', "--");
translations.put('N', "-.");
translations.put('O', "---");
translations.put('P', ".--.");
translations.put('Q', "--.-");
translations.put('R', ".-.");
translations.put('S', "...");
translations.put('T', "-");
translations.put('U', "..-");
translations.put('V', "...-");
translations.put('W', ".--");
translations.put('X', "-..-");
translations.put('Y', "-.--");
translations.put('Z', "--..");
translations.put('0', "-----");
translations.put('1', ".----");
translations.put('2', "..---");
translations.put('3', "...--");
translations.put('4', "....-");
translations.put('5', ".....");
translations.put('6', "-....");
translations.put('7', "--...");
translations.put('8', "---..");
translations.put('9', "----.");
translations.put(' ', " ");
HashMap<String, Character> reversedHashMap = new HashMap<String, Character>();
for (char key : translations.keySet()){
reversedHashMap.put(translations.get(key), key);
}Scanner scan=new Scanner(System.in);
System.out.println("Welcome to the translator. Type 1 for English to Morse or type 2 for Morse to English: ");
int choice=scan.nextInt();
if(choice==1)
engToMorse(translations);
else if(choice==2)
morseToEng(reversedHashMap);
else{
System.out.println("Invalid Input!");
}
}
public static void engToMorse(HashMap<Character,String> translations){
Scanner scan=new Scanner(System.in);
System.out.println("Please enter the sentence that you want to translate to Morse here: ");
String sentence=scan.nextLine().toUpperCase();
int i=0;
while(i<sentence.length()){
System.out.printf(translations.get(sentence.charAt(i)));
i++;
}
}
public static void morseToEng(HashMap<String,Character> reversedHashMap){
Scanner scan=new Scanner(System.in);
System.out.println("Please enter the morse code that you want to translate to english: ");
String morse=scan.nextLine().toUpperCase();
int i=0;
while(i<morse.length()){
System.out.print();
i++;
}
}
读取输入后,将输入字符串拆分 space。对于每个元素,查找摩尔斯电码(键)到字符(值)的映射并打印值。
for (String singleCode : morse.split(" ")) {
System.out.print(reversedHashMap.get(singleCode));
}
示例输入输出
Welcome to the translator. Type 1 for English to Morse or type 2 for Morse to English:
2
Please enter the morse code that you want to translate to english:
... -- ...
SMS
你可以简单地一个一个地获取莫尔斯电码,每行获取一个莫尔斯电码,处理它并把它连接成结果字符串,如果你想在一行中获取多个莫尔斯电码,在它们之间使用分隔符然后根据该拆分器拆分字符串,例如 #.
所以你会得到--#.--#..-- 然后将其拆分为:
for (String singleMorse : morse.split("#")) {
System.out.print(reversedHashMap.get(singleCode));
}
注意:因为你的摩尔斯电码中有SPACE,使用[=17以外的分离器更简单=]SPACE