0

英语到莫尔斯电码部分工作正常,但莫尔斯电码到英语没有......我不断得到 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;
                   }
                }


            }

        } 



}
    }}
4

3 回答 3

0

我认为最简单的事情是创建一个地图:

int totalLetters = 26;
HashMap<String, String> englishToMorse = new HashMap<String, String>(totalLetters);
HashMap<String, String> morseToEnglish = new HashMap<String, String>(totalLetters);

for (int i = 0; i < Alphabet.length; i++) {
    englishToMorse.put(Alphabet[i], Morse[i]);
    morseToEnglish.put(Morse[i], Alphabet[i]);
}

然后,当您需要从一个翻译到另一个时,将句子中的单词拆分,并为每个单词检查相应的字典/HashMap。

于 2014-12-06T02:50:36.593 回答
0

不要一次扫描一个字符,而是使用 StringTokenizer 或 Scanner 在空格处将其拆分。

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(Aplphabet[index]);
          break;
       }
    }
}
于 2014-12-06T02:51:12.330 回答
0

我想你在 java.util.StringTokenizer 之后

于 2014-12-06T02:51:30.000 回答