英语到莫尔斯电码部分工作正常,但莫尔斯电码到英语没有......我不断得到 E 和 T,有没有办法让如果莫尔斯电码字母之间有空格,它会知道他们是不同的字母,而不是每个“。” = E 和每个“-” =T
我试图让我的代码在从莫尔斯电码变为英语时检查空格,但它不能正常工作。
这是我的完整代码:
import java.util.Scanner;
public class MorseCode {
public static void main(String [] args){
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","1", "2","3","4","5","6","7","8","9","0"," "};
String Morse [] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..",".----","..---","...--","....-",".....","-....","--...","---..","----.","-----"," "};
//Decide Either English to Morse or Morse to English
System.out.println("For English-->Morse (Press 'E' )\n"
+ "For Mosrse-->English (Press 'M')");
Scanner Input = new Scanner(System.in);
String Response = Input.next();
//For invalid Input
while(!(Response.equalsIgnoreCase("E") || Response.equalsIgnoreCase("M"))){
System.out.println("Invalid input, Please type in E or M");
Response = Input.next();
}
//English to Morse-Code
if(Response.equalsIgnoreCase("E")){
System.out.println("Type in English");
Input.nextLine();
String Ephrase = Input.nextLine();
Ephrase = Ephrase.toUpperCase();
for(int count = 0; count < Ephrase.length(); count++ )
{
for(int index = 0; index < Alphabet.length; index++)
{
//Matches English to Morse and Prints
if(Ephrase.substring(count, (count+1)).equals(Alphabet[index]))
System.out.print(Morse[index] + " ");
}
}
}
//Morse-Code to English
else if(Response.equalsIgnoreCase("M")){
System.out.println("Type in MorseCode");
Input.nextLine();
String Mphrase = Input.next();
for( int count = 0; count < Mphrase.length(); count++ )
{
for (Scanner s = new Scanner(Mphrase); s.hasNext();) {
String letter = s.next();
for(int index = 0; index < Morse.length; index++) {
if(letter.equals(Morse[index])) {
System.out.println(Alphabet[index]);
break;
}
}
}
}
}
}}