我已经看到很多关于摩尔斯电码翻译器的问题,并且看过很多问题,但是在所有这些问题中,建议的答案都给出了相同的错误输出。代码背后的想法是能够使用数组将摩尔斯电码转换为英语,反之亦然。我的代码如下:
import java.util.Scanner;
public static void main ( String [] args )
{
String [] morse = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "|"};
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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", " "};
Scanner input = new Scanner ( System. in );
System.out.println ( "Enter 1 to translate Morse Code to English or 2 to translate English to Morse Code:");
int userChoice = input.nextInt();
String translateMe;
while (userChoice < 1 || userChoice > 2 ) // Ensures user enters a valid choice
{
System.out.println( "Not a valid entry. Enter 1 to translate Morse Code to English or 2 to translate English to Morse Code:");
userChoice = input.nextInt();
}
if (userChoice == 1 )
{
System.out.println("Please enter a Morse Code statement to translate. Separate letters with spaces and words with a ' | '" );
translateMe = input.next();
String [] morseChar = translateMe.split(" ");
for( int x = 0; x < morseChar.length; x++)
{
String letter = morseChar[ x ];
for ( int index = 0; index < morse.length; index++ )
{
if(morse [ index ].equals(letter))
{
System.out.print(english[ index ]); // Display character at matching index position of English array to show translation
}
}
}
}
else
{
System.out.println("Please enter an English statement to translate:");
translateMe = input.next();
translateMe = translateMe.toLowerCase();
String [] englishChar = translateMe.split("(?!^)");
for ( int x = 0; x < englishChar.length; x++)
{
String letter = englishChar [ x ];
for (int index = 0; index < english.length; index++)
{
if( english [index].equals( letter ))
{
System.out.print(morse[index] + " "); // Display Morse Code array character at matching index position to show translation
}
}
}
}
}
}
我一直在使用短语
to be
及其摩尔斯电码对应
- --- | -… .
作为测试用语。当我尝试用这句话将英语翻译成摩尔斯电码时我得到了
... -.
s和n
作为输出。当我尝试摩尔斯电码到英语时,我得到了
u
作为输出。我已经查看了我的两个String数组,以确保morse[A]
和english[A]
处于相同的索引位置,依此类推,这些都很好。我无法想到会导致这个问题的任何其他事情。
编辑:了解我使用IntelliJ IDEA 15
可能会有所帮助答案 0 :(得分:1)
n
("-."
)的代码在您的数组中出现两次。char[]
。您必须将字母从String[]
更改为char[]
。Map
。答案 1 :(得分:1)
由于您使用了扫描仪,从莫尔斯到英语的翻译无效。您需要使用nextLine(),如下所示:
if (userChoice == 1 )
{
translateMe = input.nextLine();
System.out.println("Please enter a Morse Code statement to translate. Separate letters with spaces and words with a ' | '" );
translateMe = input.nextLine();
之后,即使使用split命令,它似乎也可以正常转换。
输出:
[.-, -..., -.-.]
abc
输出2:
Please enter a Morse Code statement to translate. Separate letters with spaces and words with a ' | '
- --- | -... .
[-, ---, |, -..., .]
to be
答案 2 :(得分:0)
我无法理解你得到的输出 - 虽然我还没有尝试过它,但通过观察它我会看到一个非常不同的结果。
但从根本上说,你的莫尔斯语与英语翻译的匹配过早。你需要检查整个字符串。
编辑:现在没有什么显而易见的了,您是否尝试使用调试器逐步查看实际情况?
答案 3 :(得分:0)
你从英语翻译得到“s”和“n”的原因是因为如果我们检查“to”这个词,字母表中“t”的前一个字母是“s”而字母表中的前一个字母是“o”是“n”,所以这让我相信那里有一个循环错误。此外,您使用“next()”而不是“nextLine()”,因此您无法解析您的想法。我想如果你使用“nextLine()”并修复你的循环,你就会拥有它。感谢您提供完整的源代码! :d
以下是我测试的方式:
import java.util.Scanner;
public class Main {
public static void main ( String [] args )
{
String [] morse = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "|"};
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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", " "};
Scanner input = new Scanner ( System. in );
System.out.println ( "Enter 1 to translate Morse Code to English or 2 to translate English to Morse Code:");
int userChoice = input.nextInt();
String translateMe;
while (userChoice < 1 || userChoice > 2 ) // Ensures user enters a valid choice
{
System.out.println( "Not a valid entry. Enter 1 to translate Morse Code to English or 2 to translate English to Morse Code:");
userChoice = input.nextInt();
}
if (userChoice == 1 )
{
System.out.println("Please enter a Morse Code statement to translate. Separate letters with spaces and words with a ' | '" );
translateMe = input.next();
String [] morseChar = translateMe.split(" ");
for( int x = 0; x < morseChar.length; x++)
{
System.out.println("Comparing " + morseChar + " to the morse array");
for ( int index = 0; index < morse.length; index++ )
{
System.out.println("Comparing equality of: " + morseChar[x] + " and " + morse[index]);
if(morseChar[x].equals(morse[index]))
{
System.out.print(english[ index ]); // Display character at matching index position of English array to show translation
}
}
}
}
else
{
System.out.println("Please enter an English statement to translate:");
translateMe = input.next();
translateMe = translateMe.toLowerCase();
System.out.println(" Translating: [" + translateMe + "]");
System.out.println("Translating: " + translateMe);
String[] englishChar = translateMe.split(" ");
for(String s : englishChar) {
System.out.println("Translation String: " + s);
}
for ( int x = 0; x < englishChar.length; x++)
{
System.out.println("Checking word: " + englishChar[x]);
for (int index = 0; index < english.length; index++)
{
System.out.println("Comparing equality of: " + englishChar[x] + " and " + english[index] + " they are " + (englishChar [ x ].equals( english[index]) ? " equal" : " not equal"));
if( englishChar [ x ].equals( english[index]))
{
System.out.print(morse[index] + " "); // Display Morse Code array character at matching index position to show translation
}
}
}
}
}
}