-1

英語をモールス信号に変換するプログラムを作成しています。現在、ユーザー入力を取得して結果を表示するために、英語モールス (ハッシュマップを使用) と 2 つのテキスト ボックスがあります。ファイルをインポートせずに実際の変換を行う方法がわかりません。現在、2番目のアクションの実行ラベルをマップと同じに設定しています(これは間違っていると思いましたが、試す価値はありました)。文字列ビルダーを使用する必要があることは理解していますが、使用方法がわかりません。私の質問は、この問題を解決するにはどうすればよいですか?

これらは私のテキストフィールドです:

    text1.addActionListener(new ActionListener() //text box for user
    {
     public void actionPerformed(ActionEvent e)
     {
       String input = text2.getText();
       label.setText(input);
     }
   });

    button.addActionListener(new ActionListener() //convert to morse (text box)
     {
       public void actionPerformed(ActionEvent e)
       {
       String input = text1.getText();
       label.setText(charToCode);
       }
     });

これが私の英語のモールスです

//morse code
Map<Character,String> charToCode = new HashMap<Character,String>();
charToCode.put('A', ".-");
charToCode.put('B', "-...");
charToCode.put('C', "-.-.");
charToCode.put('D', "-..");
charToCode.put('E', ".");
charToCode.put('F', "..-.");
charToCode.put('G', "--.");
charToCode.put('H', "....");
charToCode.put('I', "....");
charToCode.put('J', ".---");
charToCode.put('K', "-.-");
charToCode.put('L', ".-..");
charToCode.put('M', "--");
charToCode.put('N', "-.");
charToCode.put('O', "---");
charToCode.put('P', ".--.");
charToCode.put('Q', "--.-");
charToCode.put('R', ".-.");
charToCode.put('S', "...");
charToCode.put('T', "-");
charToCode.put('U', "..-");
charToCode.put('V', "...-");
charToCode.put('W', "..-");
charToCode.put('X', "-..-");
charToCode.put('Y', "-.--");
charToCode.put('Z', "--..");

お時間をありがとうございました。

4

1 に答える 1

1

英語の文字列の各文字を調べ、マップでモールス翻訳を調べ、翻訳を文字列ビルダーのインスタンスに追加することをお勧めします。簡単な例を次に示します。

StringBuilder builder = new StringBuilder();
for(char c : englishString.getChars())
{
    builder.append(translationMap.get(c.toString().toUpperCase()));
}

System.out.println(builder.toString());

これが役立つことを願っています!

于 2015-05-31T18:31:47.013 に答える