所以我有一个作业要交,我花了很多时间试图找出问题的一个具体部分,但一直没有结果。我终于“放弃”了,决定来这里寻求帮助。
问题如下:
“编写一个程序,读取英语短语并将其编码为莫尔斯电码,或读取莫尔斯电码中的短语并将其转换为英语。在每个莫尔斯电码字母之间使用一个空格,在每个莫尔斯电码单词之间使用三个空格。”
我有点麻烦的是上面的粗体句子。我已经让程序的其余部分从英语到莫尔斯,但反过来它只是混乱。
以下是我所知道的情况:
字母“e”的莫尔斯电码是“.”和“t”是“-”,字母“a”是“.”,在我的当前代码中,如果要在莫尔斯电码转换为英语区域中输入“.”-,它将返回翻译为“et”,这是不对的。它读取每个单独的点和破折号,当它需要做的是读取整个点和破折号块,并试图找到它的等价物。我想问题中的一个空格和三个空格是我搞砸的地方,但我就是不知道如何实现。我想这意味着在一个空格之前的所有内容都将被完整地阅读,然后这三个空格将简单地翻译成一个空格,以确保英语翻译作为一个句子是可读的。
这是我现在的密码。我是一名新的Java编程学生,听说我必须用数组来回答这个问题。谢谢:
import java.util.*;
public class MorseCode {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String userResponse = "";
String english = "English";
String morse = "Morse-Code";
String phrase = "";
String answer = "";
int loop = 0;
final String[] englishArray = {"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"};
final String[] morseArray = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", " ",
".----", "..---", "...--", "....-", ".....", "-....", "--...",
"---..", "----.", "-----"};
while(loop == 0)
{
System.out.print("\nWould you like to enter a phrase in English or in Morse-code? ");
userResponse = input.next();
while(!(userResponse.equalsIgnoreCase(english) || userResponse.equalsIgnoreCase(morse)))
{
System.out.println("\nInvalid response. \nPlease enter 'English' or 'Morse-code'.\n");
System.out.print("Would you like to enter a phrase in English or in Morse-code? ");
userResponse = input.next();
}
if(userResponse.equalsIgnoreCase(english))
{
System.out.print("\nPlease enter your English phrase: ");
input.nextLine();
phrase = input.nextLine();
System.out.println("\nYou entered: " + phrase);
phrase = phrase.toUpperCase();
System.out.print("In morse code, this is: ");
for(int count = 0; count < phrase.length(); count++ )
{
for(int index = 0; index < englishArray.length; index++)
{
if(phrase.substring(count, (count+1)).equals(englishArray[index]))
System.out.print(morseArray[index] + " ");
}
}
}
else if(userResponse.equalsIgnoreCase(morse))
{
System.out.print("\nPlease enter your Morse-code phrase: ");
input.nextLine();
phrase = input.nextLine();
System.out.println("\nYou entered: " + phrase);
System.out.print("In English, this is: ");
for(int count = 0; count < phrase.length(); count++ )
{
for(int index = 0; index < morseArray.length; index++)
{
if(phrase.substring(count, (count+1)).equals(morseArray[index]))
System.out.print(englishArray[index]);
}
}
}
loop++;
System.out.print("\n\nWould you like to enter another phrase? (Y or N): ");
answer = input.next();
while(!(answer.equalsIgnoreCase("Y") || answer.equalsIgnoreCase("N")))
{
System.out.print("\nIncorrect input. Please enter either 'Y' or 'N'.");
System.out.print("Would you like to create 20 sentences? (Y or N): ");
answer = input.next();
}
if(answer.equalsIgnoreCase("Y"))
{
loop = 0;
}
else
{
System.out.println("Program ended.");
input.close();
}
}
}
}
最佳答案
所以莫尔斯语中的一个单词看起来像这样:
.- -... -.-.
意思是
ABC
。注意,每个莫尔斯字符之间都有空格。您应该尝试对输入进行标记化,在这种情况下,只需在' '
String input = readFromSomewhere();
// individual characters, you can now look them up in your array.
String [] morseCharacters = input.split(" ");
另外,如果输入的单词不止一个,那么你应该首先得到单个单词,从每个单词中你可以得到单个莫尔斯字符:
String multiWordInput = readFromSomewhere();
String [] words = multiWordInput.split(" "); // 3 spaces between words
for (String word : words) {
String [] morseChars = words.split(" ");
// Character can be translated.
}
对于存储单个字符,我认为
Map
数据结构更易于使用(可能这是不允许的),但不需要线性搜索来查找每个字母的翻译:Map <String, String> morseToAscii = new HashMap<String, String>();
morseToAscii.put(".-", "A");
morseToAscii.put("-...", "B");
morseToAscii.put("-.-.", "C");
// ...
这会把你的莫尔斯电文映射成英文字母,所以你在收到这些字母后会像这样查找它们:
String letterA = morseToAscii.get(".-"); // returns "A"
String letterB = morseToAscii.get("-..."); // returns "B"
String letterC = morseToAscii.get("-.-."); // returns "C"
String notValid = morseToAscii.get("Not a morse letter"); // this will be null
另外,我不知道你是否有任何面向对象的背景,但为了避免代码变得不可读,我会将翻译逻辑与实际的控制台阅读分离,等等…
如果我是你,我会创建一个通用的界面来翻译东西:
interface Translator {
String translate(String input);
String translateCharacter(String character);
String translateWord(String word);
}
然后在morse->英语和英语->morse翻译中实现:
class MorseToEnglishTranslator implements Translator {
// implement the Morse -> English translation here
}
以及
class EnglishToMorseTranslator implements Translator {
// implement the English -> Morse translation here
}
这样你就可以更好地组织代码:)