2016-02-26 49 views
1

我見過很多關於摩爾斯電碼翻譯器的問題,並且看過很多,但是在所有這些問題中,建議的答案都會給我提供相同的錯誤輸出。代碼背後的想法是能夠使用數組將Morse Code翻譯成英文,反之亦然。我的代碼如下:爲什麼我的Java Morse Code翻譯器給我錯誤的輸出?

import java.util.Scanner; 
public static void main (String [] args) 
{ 

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

    String [] english = { "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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", " "}; 

    Scanner input = new Scanner (System. in); 
    System.out.println ("Enter 1 to translate Morse Code to English or 2 to translate English to Morse Code:"); 
    int userChoice = input.nextInt(); 
    String translateMe; 

    while (userChoice < 1 || userChoice > 2) // Ensures user enters a valid choice 
    { 
     System.out.println("Not a valid entry. Enter 1 to translate Morse Code to English or 2 to translate English to Morse Code:"); 
     userChoice = input.nextInt(); 
    } 

    if (userChoice == 1) 
    { 
     System.out.println("Please enter a Morse Code statement to translate. Separate letters with spaces and words with a ' | '"); 
     translateMe = input.next(); 

     String [] morseChar = translateMe.split(" "); 

     for(int x = 0; x < morseChar.length; x++) 
     { 
      String letter = morseChar[ x ]; 
      for (int index = 0; index < morse.length; index++) 
      { 
       if(morse [ index ].equals(letter)) 
       { 
        System.out.print(english[ index ]); // Display character at matching index position of English array to show translation 
       } 
      } 
     } 

     } 

    else 
    { 
     System.out.println("Please enter an English statement to translate:"); 
     translateMe = input.next(); 
     translateMe = translateMe.toLowerCase(); 

     String [] englishChar = translateMe.split("(?!^)"); 

     for (int x = 0; x < englishChar.length; x++) 
     { 
      String letter = englishChar [ x ]; 

      for (int index = 0; index < english.length; index++) 
      { 
       if(english [index].equals(letter)) 
       { 
        System.out.print(morse[index] + " "); // Display Morse Code array character at matching index position to show translation 
       } 

      } 
     } 

    } 

} 

}

我一直用這句話

to be

和摩爾斯電碼對應

- --- | -… .

作爲測試短語。當我嘗試用這句話翻譯英語莫爾斯電碼,我得到

... -. S和N

作爲輸出。當我嘗試莫爾斯電碼的英文,我得到

u

作爲輸出。我已經通過我的兩個字符串數組,以確保morse[A]english[A]處於相同的索引位置等等,這些都很好。我想不出任何會導致這個問題的東西。

編輯:這可能是有益的知道自己使用的IntelliJ IDEA 15

回答

1

由於您使用了掃描儀,莫爾斯語翻譯成英語不起作用。您需要使用nextLine()如下:

if (userChoice == 1) 
{ 
    translateMe = input.nextLine(); 
    System.out.println("Please enter a Morse Code statement to translate. Separate letters with spaces and words with a ' | '"); 
    translateMe = input.nextLine(); 

之後,似乎翻譯罰款,甚至與拆分命令。

輸出:

[.-, -..., -.-.] 
abc 

輸出2:

Please enter a Morse Code statement to translate. Separate letters with spaces and words with a ' | ' 
- --- | -... . 
[-, ---, |, -..., .] 
to be 
+0

這適用於英語莫爾斯,但莫爾斯英語我錯過了'b' – Jes

+0

對不起傑斯,你是什麼意思錯過'B'? –

+0

啊,對不起,我的'要'測試(從莫爾斯到英文)的'b'缺失。我一直在用它作爲測試短語。 – Jes

0

我不明白你得到的輸出 - 雖然我還沒有嘗試運行它,目測它,我看到一個很不同的結果。

雖然從根本上說,你的莫爾斯到英語的翻譯者太早匹配了。你需要檢查整個字符串。

編輯:現在沒有什麼明顯的突出,你有沒有試過用調試器來查看實際發生了什麼?

+0

我的道歉,我已經貼了我的代碼以前的版本。我已經編輯了我目前所擁有的原始代碼。這個版本(我認爲)貫穿整個用戶輸入。但是,這兩個版本都會給我提供相同的錯誤輸出。 – Jes

+0

調試器給我' - ---從目標虛擬機斷開,地址:'127.0.0.1:61223',transport:'socket''但我真的不知道這意味着什麼。 – Jes

1
  1. 代碼爲n"-.")在您的數組中出現兩次。
  2. 您可能更適合將字符串轉換爲char[]。您必須將字母從String[]更改爲char[]
  3. 這就是爲什麼我們有Map
+0

謝謝!我想我錯過了盯着它那麼久。我現在得到'to'和英文輸出't'的莫爾斯輸出,所以我的翻譯過早停下來。有任何想法嗎? – Jes

+0

查看@Ian的評論。這將解釋缺失的輸出。 – bradimus

0

你是從英文翻譯得到的 「s」 和 「n」 的原因是因爲如果我們考察單詞 「到」字母表中的前一個字母爲「t」爲「s」,而字母表中的前一個字母爲「o」爲「n」,因此這導致我認爲其中存在循環錯誤。另外,你使用「next()」而不是「nextLine()」,所以你不能解析你的想法。我想如果你使用「nextLine()」並修復你的循環,你就會擁有它。感謝您提供完整的源代碼! :d

以下是我測試:

import java.util.Scanner; 

公共類主要{

public static void main (String [] args) 
{ 

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

    String [] english = { "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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", " "}; 

    Scanner input = new Scanner (System. in); 
    System.out.println ("Enter 1 to translate Morse Code to English or 2 to translate English to Morse Code:"); 
    int userChoice = input.nextInt(); 
    String translateMe; 

    while (userChoice < 1 || userChoice > 2) // Ensures user enters a valid choice 
    { 
     System.out.println("Not a valid entry. Enter 1 to translate Morse Code to English or 2 to translate English to Morse Code:"); 
     userChoice = input.nextInt(); 
    } 

    if (userChoice == 1) 
    { 
     System.out.println("Please enter a Morse Code statement to translate. Separate letters with spaces and words with a ' | '"); 
     translateMe = input.next(); 

     String [] morseChar = translateMe.split(" "); 

     for(int x = 0; x < morseChar.length; x++) 
     { 
      System.out.println("Comparing " + morseChar + " to the morse array"); 
      for (int index = 0; index < morse.length; index++) 
      { 
       System.out.println("Comparing equality of: " + morseChar[x] + " and " + morse[index]); 
       if(morseChar[x].equals(morse[index])) 
       { 
        System.out.print(english[ index ]); // Display character at matching index position of English array to show translation 
       } 
      } 
     } 

    } 

    else 
    { 
     System.out.println("Please enter an English statement to translate:"); 
     translateMe = input.next(); 
     translateMe = translateMe.toLowerCase(); 
     System.out.println(" Translating: [" + translateMe + "]"); 

     System.out.println("Translating: " + translateMe); 
     String[] englishChar = translateMe.split(" "); 

     for(String s : englishChar) { 
      System.out.println("Translation String: " + s); 
     } 

     for (int x = 0; x < englishChar.length; x++) 
     { 
      System.out.println("Checking word: " + englishChar[x]); 
      for (int index = 0; index < english.length; index++) 
      { 
       System.out.println("Comparing equality of: " + englishChar[x] + " and " + english[index] + " they are " + (englishChar [ x ].equals(english[index]) ? " equal" : " not equal")); 
       if(englishChar [ x ].equals(english[index])) 
       { 
        System.out.print(morse[index] + " "); // Display Morse Code array character at matching index position to show translation 
       } 

      } 
     } 

    } 

} 

}

相關問題