2017-02-24 246 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]); 
    } 
}