所以程序的對象是在標題。我已經成功地將英語轉換成莫爾斯電碼,但相反的方式證明是困難的。我已經瀏覽了所有的論壇,並沒有能夠解決我的問題在這裏。它似乎可以轉換,它只是轉換莫爾斯電碼的最後一個字符。我也無法想出使用空格區分單詞的方法。任何幫助將不勝感激。莫爾斯英語
/******
* This program will prompt the user to specify the desired type of translation,
* input a string of Morse code characters or English characters,
* then display the translated results
*
* Pre-Conditions:
*
* Post-Conditions:
*
* @author: PC
******/
import java.util.Scanner;
import java.util.Arrays;
public class MorseCode
{
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
String userInput;
System.out.println("This is a Translator.");
System.out.println("Would you like to translate sentence to Morse code or English? (MC or E):");
String choice = input.nextLine();
choice = choice.toLowerCase();
if(choice.equals("e")){
System.out.println("Enter sentence (puncuation not needed):");
userInput = input.nextLine();
userInput = userInput.toLowerCase();
String [] morseWords = userInput.split(" ");
convertToEnglish(morseWords);
}
else if(choice.equals("mc")){
System.out.println("Enter sentence (puncuation not needed):");
userInput = input.nextLine();
convertToMorse(userInput);
}
}
public static void convertToMorse(String text)
{
int i, j;
String [] morseAns = new String[text.length()];
text = text.toLowerCase();
String alphabet = ("abcdefghijklmnopqrstuvwxyz1234567890 ");
String [] morse = new String[] { ".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--",
"-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..",
".----","..---","...--","....-",".....","-....","--...","---..","----.","-----", "|" };
char selectedChar, convertedChar, alphabetChar;
for(i = 0; i < text.length(); i++)
{
selectedChar = text.charAt(i);
for(j = 0; j < alphabet.length(); j++)
{
alphabetChar = alphabet.charAt(j);
if(selectedChar == alphabetChar)
{
morseAns[i] = morse[j];
}
}
}
for(i = 0; i < text.length(); i++)
{
System.out.print(morseAns[i] + " ");
}
}
public static void convertToEnglish(String [] multiMorseWords)
{
int i, j;
String multipleMorseWords;
String alphabet = ("abcdefghijklmnopqrstuvwxyz1234567890 ");
String [] morse = new String[] { ".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--",
"-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..",
".----","..---","...--","....-",".....","-....","--...","---..","----.","-----", "|" };
char [] englishAns = new char[multiMorseWords.length];
for(i = 0; i < multiMorseWords.length; i++)
{
multipleMorseWords = multiMorseWords[i];
String [] morseChars = multipleMorseWords.split(" ");
for(j = 0; j < morseChars.length; j++)
{
if(morseChars[j].equals(morse[j]))
{
englishAns[i] = alphabet.charAt(j);
}
}
}
for(i = 0; i < multiMorseWords.length; i++)
{
System.out.println(englishAns[i]);
}
}
}
的[轉換摩爾斯電碼,以英文文本與Java]可能的複製(http://stackoverflow.com/questions/29572916/converting-morse-code-to-english-text- with-java) –
如果你的當前輸出與你想要的輸出不匹配,並且你不知道爲什麼,那麼是時候開始調試了。如果你不確定如何去做這件事,那麼請看看:[如何調試小程序](http://ericlippert.com/2014/03/05/how-to-debug-small-程式/)。它不會解決你的直接問題,但它會給你你可以遵循的步驟,應該可以幫助你自己解決它,或者即使這樣做不成功,那麼至少可以幫助你更好地隔離你的問題,以便你的問題可以更專注,更容易回答。 –