【发布时间】:2014-09-18 05:49:54
【问题描述】:
所以我有一个作业到期,我花了很长时间试图找出问题的特定部分,但一直没有提出任何问题。我终于“放弃”了,决定来这里寻求一点帮助。
问题如下:
“编写一个程序,读取一个英语短语并将该短语编码为摩尔斯电码,或者读入一个摩尔斯电码短语并将该短语转换为英语。在每个摩尔斯电码字母之间使用一个空格每个摩尔斯电码词之间有三个空格。”
我遇到的问题是上面的粗体句子。我已经让程序的其余部分从英语到莫尔斯语工作,但反过来它只是搞砸了。
这是我所知道的情况:
字母 'e' 的摩尔斯电码是 '.' 而 't' 是 '-',而字母 'a' 是 '.-',在我当前的代码中,如果您要输入 '.-'在 morse-to-english 区域,它会将翻译返回为 '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();
}
}
}
}
【问题讨论】:
-
这里有一个提示 - 如果在你的 morseArray 中你在每个字符串之后包含一个空格,例如“.-”而不是“.-”
-
看看 String.split() 和正则表达式。您想通过任意数量的空格字符 (\\s+) 将英语句子拆分为单词,然后遍历每个单词的每个字符。莫尔斯文本你想用 3 个空格 (\\s{3}) 分割成单词,然后用一个空格 (\\s) 将单词分割成字符
-
Kharyam - 尝试过,它导致输出完全空白! Radai - 现在研究 String.split()。我们没有在课堂上讨论过这个问题,但它似乎确实可以解决这个问题。谢谢!
标签: java arrays for-loop while-loop nested-loops