0

私は、モールス信号を英語に、またはその逆に翻訳できるプロジェクトに取り組んでいます。以下は具体的な指示です: 「あなたのプログラムは、ユーザーに目的の翻訳の種類を指定するように促し、モールス符号文字または英字の文字列を入力し、翻訳結果を表示する必要があります。モールス符号を入力するときは、各文字/数字を"|" で複数の単語を区切ります. たとえば、- --- | -... . は "to be" という文のモールス符号入力になります. プログラムは 1 つの文を処理するだけで済みます.句読点を無視できます。」

英語をモールス信号に変換する方法はわかりましたが、モールス信号を英語に変換する方法がわかりません。そして、あなたがこれを読んでいるなら、私を助けてください!ヘルプやヒントをいただければ幸いです。ありがとう :)

public static String[] morse = { ".- ", "-... ", "-.-. ", "-.. ", ". ",
        "..-. ", "--. ", ".... ", ".. ",

        ".--- ", "-.- ", ".-.. ", "-- ", "-. ", "--- ", ".--. ", "--.- ",
        ".-. ", "... ", "- ", "..- ",

        "...- ", ".-- ", "-..- ", "-.-- ", "--.. ", "|" };

public static 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", " " };

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Input '1' to translate English to Morse Code.");
    System.out.println("Input '2' to translate Morse Code to English.");
    int kind = in.nextInt();

    if (kind == 1) {
        System.out.println("Please insert an alphabet string");
        String Eng = in.next();
        EngToMo(Eng);
    }
    if (kind == 2) {
        System.out.println("Please insert a morse string");
        String Mor = in.next();
        MoToEng(Mor);
    }

}

public static void EngToMo(String string1) {
    String Upper1 = string1.toUpperCase();
    for (int i = 0; i < Upper1.length(); i++) {
        char x = Upper1.charAt(i);
        if (x != ' ') {
            System.out.print(morse[x - 'A'] + " ");
        } else {
            System.out.println(" | ");
        }
    }
}

public static void MoToEng(String string2) {

    }
}
4

3 に答える 3

1

Hashtable を使用して辞書を作成することをお勧めします。この場合、アルファベットをキーとして使用でき、関連するモールス信号をこのキーと組み合わせることができます。一意の Key-Value ペアが必要な場合は、ストレージにBiMapを使用できます。

Map<String, String> codeMap = new HashMap<String, String>();
codeMap.put("A", ".- ");
codeMap.put("B", "-... ");

このマップに簡単にアクセスして、キーまたは値を取得できます

for (String key: codeMap.keySet() ){
    System.out.println(key+" : "+codeMap.get(key) );
}
于 2015-01-19T06:21:21.447 に答える
0

入力文字列を分割して、その内容を解析できます...

public static void MoToEng(String string2) {
    String[] splits = string2.split(" "); //you split the string into
                                           //String[] - delimiter = ' '


    String literal = ""; //this will be your result

    for (String split: splits){ //iterate through the String[] split

        splits = splits + " "; //your morse code ends with
                               //a blank - you have to add that 
                               //else equals() won't find 
                               //a suitable match
        //then look into your morse code
        for (int index = 0; index < morse.length(); index++){ 
            string code = morse[index];

            //if split looks like a morse code, you found your 
            //character
            if (split.equals(code){

                //translate back then
                literal = literal + ('A'+index);

            }
        }
    }
    System.out.println(literal);
}
于 2015-01-19T06:20:44.247 に答える
0

Map<String,String>の保存にa を使用しないのはなぜですか?<morsecode,english alphabet>

HashMap<String,String> map=new HashMap<String,String>();
//populate map
map.put(".- ","a");
map.put("-... ","b");

そしてメソッドでEngToMo(String string1)

public static void EngToMo(String string1){

for (Entry<String, String> entry : map.entrySet()) {
            if (entry.getValue().equals(string1)) {
                System.out.println(entry.getKey());
            }
        }
}

そしてメソッドでMoToEng(String string2)

public static void MoToEng(String string2){
 System.out.println(map.get(string2));
}
于 2015-01-19T06:34:43.107 に答える