莫尔斯英语

时间:2017-02-24 02:52:57

标签: java

所以程序的对象在标题中。我成功地将英语转换为摩尔斯电码,但事实证明是相反的。我在整个论坛上看了一遍,但在这里无法解决我的问题。它似乎可以转换,它只是转换莫尔斯代码的最后一个字符。我也无法想出一种使用空格来区分单词的方法。任何帮助将不胜感激。

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


}

2 个答案:

答案 0 :(得分:1)

您可以创建一个地图并将所有键添加为莫尔斯字符串,将值添加为字母,然后迭代单词并从地图中获取。你需要处理一些事情,例如每个莫尔斯代码都应该被某些东西分隔,然后单词应该是不同的。就像你当前有一个单词的3个空格一样,你必须划分morse值的间距,否则我看不出区别的方法。

这是一个例子:

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));    
    }
}

答案 1 :(得分: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]);
  }
}