2017-02-24 242 views
-2

所以程序的對象是在標題。我已經成功地將英語轉換成莫爾斯電碼,但相反的方式證明是困難的。我已經瀏覽了所有的論壇,並沒有能夠解決我的問題在這裏。它似乎可以轉換,它只是轉換莫爾斯電碼的最後一個字符。我也無法想出使用空格區分單詞的方法。任何幫助將不勝感激。莫爾斯英語

/****** 
* 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]); 
     } 
    } 


} 
+0

的[轉換摩爾斯電碼,以英文文本與Java]可能的複製(http://stackoverflow.com/questions/29572916/converting-morse-code-to-english-text- with-java) –

+1

如果你的當前輸出與你想要的輸出不匹配,並且你不知道爲什麼,那麼是時候開始調試了。如果你不確定如何去做這件事,那麼請看看:[如何調試小程序](http://ericlippert.com/2014/03/05/how-to-debug-small-程式/)。它不會解決你的直接問題,但它會給你你可以遵循的步驟,應該可以幫助你自己解決它,或者即使這樣做不成功,那麼至少可以幫助你更好地隔離你的問題,以便你的問題可以更專注,更容易回答。 –

回答

1

您只需創建一個地圖,並添加所有鍵的莫爾斯電字符串和值的字母,然後遍歷單詞和從地圖得到。有一些事情你必須處理,例如每個莫爾斯碼應該由某些東西劃定界限,然後單詞應該是不同的。就像你目前有一個單詞有3個空格,你將不得不劃定間距的莫爾斯值,否則我沒有看到一種方式來區分。

這裏有一個例子:

public static void convertToEnglish(String[] multiMorseWords) { 


     Map<String, String> morse = new HashMap<String, String>(); 
     morse.put(".-", "a"); 
     morse.put("-...", "b"); 
     // etc.. add all the morse code inputs 

    for (String word : multiMorseWords) { 
     System.out.println(morse.get(word));  
    } 
} 
0

修正我的代碼!在main方法中不需要調用split(),只需要在convertToEnglish方法中分割即可。這裏是我的修訂方法:

public static void convertToEnglish(String morseSentence) 
{ 
    int i, j; 

    String alphabet = ("abcdefghijklmnopqrstuvwxyz1234567890 "); 

    String [] morse = new String[] { ".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--", 
    "-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..", 
    ".----","..---","...--","....-",".....","-....","--...","---..","----.","-----", "|" };  

    String [] morseChars = morseSentence.split(" "); 
    String selectedMorse; 
    char [] englishAns = new char[morseChars.length]; 

    for(i = 0; i < morseChars.length; i++) 
    { 
    selectedMorse = morseChars[i]; 

    for(j = 0; j < morse.length; j++) 
    { 
     if(selectedMorse.equals(morse[j])) 
     { 
     englishAns[i] = alphabet.charAt(j); 
     } 
    } 
    } 

    for(i = 0; i < englishAns.length; i++) 
    { 
    System.out.print(englishAns[i]); 
    } 
}