私はモールス・コードに問題があります。ユーザーが単なるモールス・コード以上のものを入力したときには、単一の疑問符を印刷したいと思っています。である必要があり、現在、私はそれだけで次の行にスキップ持って、モールス・コードをモールスに翻訳する
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を返すとき。 – DwB