私は、プログラミング入門クラスの簡単なモールス符号翻訳に取り組んでいます。これは私が教えられたテクニックに基づいた非常にシンプルなデザインです。
このプログラムは、1 文字の変換には機能しますが、単語や文には変換できません。問題はmorse[index]
最後のステートメントに関係していると思いますが、翻訳されたテキスト全体を印刷する方法がわかりません。
public class Exercise12_9
{
public static void main(String[] args)
{
String[] english = { "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 = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.", "---", ".---.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----",
"..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.",
"-----", "--..--", ".-.-.-", "..--.." };
Scanner keyboard = new Scanner(System.in);
String userInput;
int index;
index = 0;
System.out.println(" This is an English to Morse Code Translator. ");
System.out.println(" Please enter what you would like translate ");
System.out.println(" into Morse Code. ");
System.out.println(" ============================================ ");
userInput = keyboard.next();
userInput = userInput.toLowerCase();
for (index = 0; index < userInput.length(); index++)
{
char [] chars = userInput.toCharArray();
if (userInput.equals(english[index]))
{
System.out.println(" Translated : " + morse[index]);
}
}
}
}