【发布时间】:2017-11-20 19:51:18
【问题描述】:
我的莫尔斯电码有点问题,特别是当用户输入的不仅仅是莫尔斯电码(即 .-asd)时,我希望它打印出一个问号,目前输出应该是“?”我让它跳到下一行,
import java.util.Scanner;
public class Morse {
static String[] MORSE = {
".-" ,"-...","-.-.","-.." ,"." , //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 input = new Scanner(System.in);
String line;
while(!(line = input.nextLine().toUpperCase()).equals("*")){
String[] words = line.split(" ");
String output = "";
for(int i = 0; i< words.length; ++i){
if(words[i].length() == 0){
output += " ";
continue;
}
if(words[i].charAt(0) == '-' || words[i].charAt(0) == '.'){ //if it begins as morse code
for (int d = 0; d < MORSE.length; ++d) {
if(words[i].equals(MORSE[d]))
output += (char)('A'+d);
} //i wanted here to make a condition where if it starts as morse and follows with other things other than morse print out a single "?".
} else System.out.print("?") //prints ("?") if its not morse code
【问题讨论】:
-
使用
Map将莫尔斯电码字符串转换为字母。打印“?”当 map.get(morseCodeString) 返回 null 时。
标签: java morse-code