2016-03-01 134 views
0

我將以一個事實說明這個問題,即我只有1個月的時間學習編程,而這個學校任務讓我難住了。具體來說,它的摩爾斯電碼英語翻譯(反之亦然)......這裏是我堅持的部分上:試圖從字符串數組中創建一個字符串

/* 
* A program that asks the user to select which they would like to do: translate 
* from English to Morse code, or Morse code to English. The list of characters 
* for each language is stored using arrays. The program will then perform and return 
* the translations. 
*/ 
import java.util.Scanner; 

public class MorseEnglishTranslator 
{ 

    public static void main(String [] args) 
    { 
     int translateChoice = 0;     // Variable for person's choice for direction of translation 

     Scanner inputText = new Scanner(System.in); // Create a Scanner to obtain user input 
     System.out.print("Enter 1 to translate from English to Morse code, 2 for Morse code to English: "); 
     translateChoice = inputText.nextInt(); 

     if (translateChoice == 1); 
      {   
       System.out.print("Enter a letter, word, or phrase you would like translated: "); 
      }  
     if (translateChoice == 2); 
      { 
       System.out.print("Enter the Morse code you would like translated, separate letters and words with a |: "); 
      } 
     String userStr = inputText.nextLine(); 

     translator(translateChoice, userStr); 


    } // Closes main 

    public static void translator(int translateChoice, String userStr) // Method for translating either direction 
    { 
     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", "1", "2", "3", "4", "5", "6", 
          "7", "8", "9", "0", " "}; 

     String s1 = String.join(" ", english); // Assigns the contents of english array to one string 

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

     String s2 = String.join("|", morse); // Assigns the contents of morse array to one searchable string 

     if (translateChoice == 1); 
      {   
       String userStrLower = userStr.toLowerCase(); // In case user capitalized anything, changes everything to lowercase 
       for (int allFound=0; allFound <userStrLower.length(); allFound ++) 
       { 
        allFound = s1.indexOf(userStrLower); // Variable 'allFound' to receive the search of s1 using userStrLower 
        System.out.print(allFound); 
       } 
      }  
     if (translateChoice == 2); 
      { 
       for (int allFound=0; allFound <userStr.length(); allFound ++) 
       { 
        allFound = s2.indexOf(userStr); // Variable 'allFound' to receive the search of s2 using userStr 
        System.out.print(allFound); 
       } 
      } 

    } // Closes translator   
} // Closes class 

字符串S1和S2的兩個選擇我用考慮,但S1版在New之後告訴我有一個預期的分號。連接選項,我試圖作爲String.join與連接,說沒有合適的構造函數vs無法找到符號分別。

+2

請註明您所使用的語言。 – sschale

+1

對不起,Java 8 –

+0

已經爲她完成了,她只需要接受! – sschale

回答

-1

我明白你是在Java類的介紹中,所以隨時提問,但你1)不需要加入字符串和2)indexOf真的是效率低下,你需要它的目的。 A HashMap<String, String>是翻譯者的完美對象。

我已經制作了這個雙向映射類,它可以在兩個字符串數組之間來回切換。將其保存到BiMap.java文件中。

import java.util.HashMap; 

public class BiMap { 
    private HashMap<String, String> forwardMap; 
    private HashMap<String, String> backwardMap; 

    public BiMap(String[] from, String[] to) { 
     forwardMap = new HashMap<>(); 
     backwardMap = new HashMap<>(); 

     for (int i = 0; i < from.length && i < to.length; i++) { 
      forwardMap.put(from[i], to[i]); 
      backwardMap.put(to[i], from[i]); 
     } 
    } 

    public String translateForward(String key) { 
     return forwardMap.get(key); 
    } 

    public String translateBackward(String key) { 
     return backwardMap.get(key); 
    } 
} 

下面是該類的示例用法。隨意將您的用戶提示添加回它。建議對類變量常量使用static final修飾符。然後,我定義了一些private方法來提取出翻譯邏輯。

這是一個很好的習慣,在學習時儘可能多地編寫可讀和可重用的方法。

public class MorseEnglishTranslator { 

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

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

    private static final BiMap MORSE_MAP = new BiMap(ENGLISH, MORSE); 

    private static final String DELIMITER = " "; 

    private static String toMorse(String s) { 
     StringBuilder sb = new StringBuilder(); 
     String lower = s.toLowerCase(); 
     for (int i = 0; i < lower.length(); i++) { 
      String c = String.valueOf(lower.charAt(i)); 
      sb.append(MORSE_MAP.translateForward(c)).append(DELIMITER); 
     } 
     return sb.toString(); 
    } 

    private static String fromMorse(String s) { 
     String[] split = s.split(DELIMITER); 
     StringBuilder sb = new StringBuilder(); 
     for (String morse : split) { 
      sb.append(MORSE_MAP.translateBackward(morse)); 
     } 
     return sb.toString(); 
    } 

    public static void main(String[] args) { 
     String sentence = "Hello World"; 
     String morse = toMorse(sentence); 
     String translated = fromMorse(morse); 

     System.out.println(sentence); 
     System.out.println(morse); 
     System.out.println(translated); 
     System.out.println(sentence.equalsIgnoreCase(translated)); 

    } 

} 

採樣運行

Hello World 
.... . .-.. .-.. --- | .-- --- .-. .-.. -.. 
hello world 
true 
+0

再一次,非常感謝你的幫助,但是就像我告訴東勝一樣,我不得不使用數組,更不用說我們沒有通過HashMap或者讀取外部文件。 –

+0

我沒有使用數組,我只是使用數組創建了一個Hashmap。也根本不讀取外部文件,只做了一個單獨的課程。你已經使用過課程,即使你還沒有學過他們的課程 –

+0

對不起,我沒有意識到,這只是我們沒有理解任何一個概念,所以我會充滿作弊。我們即將開始有多個班級。無論如何,這是非常好的,只是不是我可以包裹頭腦的東西,很明顯,我仍然有這些「低級」概念的問題。我會說,似乎試圖使這個工作只使用數組和字符串似乎過於複雜,相比之下的替代品。 :) –