2014-06-11 147 views
1

從莫爾斯翻譯成英文就像是一種魅力,但是將短語或句子(用空格分隔的多個單詞)從英語翻譯成莫爾斯只會產生翻譯成莫爾斯的第一個單詞。例如,如果我輸入'Hello World',翻譯人員只會返回英語 - 莫爾斯翻譯器

'.... .- .. .- .. ---'。

爲了從翻譯者處獲得完整的句子,我需要改變什麼?我已經指出我認爲問題在哪裏,並且注意翻譯者忽略標點符號。 TIA

import java.util.Scanner; 

public class JavaProgram 
{ 
    //made them static so they can be accessed from static functions 
    private final static char[] ABC = {'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', ' '}; 

    private final static String[] MORSE = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", 
     ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", 
     "...-" ,".--" ,"-..-", "-.--", "--..", "|"}; 

    public static void main(String [] args) 
    {  
     Scanner scan = new Scanner(System.in); 
     System.out.print("If you want to convert from Morse to English, type 'M', if you would like to convert from English to Morse, type 'E': "); 
     String answer = scan.nextLine(); 
     if(answer.equals("E")) 
     { 
      Scanner scan2 = new Scanner(System.in); 
      System.out.print("Enter a word or phrase in English: "); 
      String englishInput = scan.next(); 
      System.out.println(toMorse(englishInput)); 
      scan2.close(); 
     } 
     if (answer.equals("M")) 
     {   
      Scanner scan3 = new Scanner(System.in); 
      System.out.print("Enter a word or phrase in Morse: "); 
      String morseInput = scan.nextLine(); 
      System.out.println(getABC(morseInput)); 
      scan3.close(); 
       } 
     scan.close(); 
     } 


    private static String toMorse(String ABCString) 
    { 
     String morseString = ""; 
     for (char c : ABCString.toLowerCase().toCharArray()) 
     { 
      morseString += charToMorse(c) + " "; 
     } 
     return morseString; 
    } 

    //**I am fairly certain the problem is in this method.** 
    private static String charToMorse(char c) 
    { 
     for(int i = 0; i < ABC.length; ++i) 
     { 
      if (c == ABC[i]) 
      { 
       return MORSE[i]; 
      } 
     } 
     return String.valueOf(c); 
    } 


    private static String getABC(String morseString) 
    { 
     String ABCString = ""; 
     for (String s : morseString.split("\\s")) 
     { 
      ABCString += characterToABC(s); 
     } 
     return ABCString; 
    } 


    private static String characterToABC(String s) 
    { 
     for (int i = 0; i < MORSE.length; ++i) 
     { 
      if (s.equals(MORSE[i])) 
      { 
       return String.valueOf(ABC[i]); 
      } 
     } 
     return s; 
    } 
} 
+0

'Scanner#next()'做了什麼? 'Scanner#nextLine()'做了什麼? –

回答

4

我相信,基於http://www.tutorialspoint.com/java/util/scanner_next.htm(java.util.Scanner.next的定義),您String englishInput = scan.next();線在本節:

if(answer.equals("E")) 
{ 
    Scanner scan2 = new Scanner(System.in); 
    System.out.print("Enter a word or phrase in English: "); 
    String englishInput = scan.next(); 
    System.out.println(toMorse(englishInput)); 
    scan2.close(); 
} 

只讀取第一個記號,這是第一個單詞,因爲單個空格是空白的,而不是nextLine(),它正確接受了整個句子。