Ich habe ein bisschen ein Problem mit meinem Morse-Code, insbesondere ich will es ein einzelnes Fragezeichen ausdrucken, wenn der Benutzer mehr als nur Morse-Code, dh.-Asd, die Ausgabe gibt sein sollte, zur Zeit habe ich es in die nächste Zeile nur überspringen,Morse-Code übersetzen Morse in Sprache Java
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
Verwenden Sie eine 'Map', um aus der Morse-Code-Zeichenfolge in den Buchstaben zu übersetzen. Drucken Sie das "?" wenn die map.get (morseCodeString) null zurückgibt. – DwB