簡體   English   中英

無法將莫爾斯語翻譯成英語

[英]Unable to translate morse to english

我有一個學校項目,我需要將莫爾斯語翻譯成英語。 我是 Java 的新手,所以我想知道您是否可以幫助我。 問題是我不確定我應該在while循環中放置什么以將莫爾斯電碼轉換為英文。 任何幫助,將不勝感激。 謝謝^^

public static void main(String[]args){
    HashMap<Character,String> translations=new HashMap<Character,String>();
    translations.put('A', ".-");
    translations.put('B', "-...");
    translations.put('C', "-.-.");
    translations.put('D', "-..");
    translations.put('E', ".");
    translations.put('F', "..-.");
    translations.put('G', "--.");
    translations.put('H', "....");
    translations.put('I', "..");
    translations.put('J', ".---");
    translations.put('K', "-.-");
    translations.put('L', ".-..");
    translations.put('M', "--");
    translations.put('N', "-.");
    translations.put('O', "---");
    translations.put('P', ".--.");
    translations.put('Q', "--.-");
    translations.put('R', ".-.");
    translations.put('S', "...");
    translations.put('T', "-");
    translations.put('U', "..-");
    translations.put('V', "...-");
    translations.put('W', ".--");
    translations.put('X', "-..-");
    translations.put('Y', "-.--");
    translations.put('Z', "--..");
    translations.put('0', "-----");
    translations.put('1', ".----");
    translations.put('2', "..---");
    translations.put('3', "...--");
    translations.put('4', "....-");
    translations.put('5', ".....");
    translations.put('6', "-....");
    translations.put('7', "--...");
    translations.put('8', "---..");
    translations.put('9', "----.");
    translations.put(' ', "   ");
    
    HashMap<String, Character> reversedHashMap = new HashMap<String, Character>();
    for (char key : translations.keySet()){
        reversedHashMap.put(translations.get(key), key);
    }Scanner scan=new Scanner(System.in);
    System.out.println("Welcome to the translator. Type 1 for English to Morse or type 2 for Morse to English: ");
    int choice=scan.nextInt();
    if(choice==1)
       engToMorse(translations);
    else if(choice==2)
        morseToEng(reversedHashMap);
    else{
        System.out.println("Invalid Input!");
    }
}
public static void engToMorse(HashMap<Character,String> translations){
    
    Scanner scan=new Scanner(System.in);
    System.out.println("Please enter the sentence that you want to translate to Morse here: ");
    String sentence=scan.nextLine().toUpperCase();
    int i=0;
    while(i<sentence.length()){
        System.out.printf(translations.get(sentence.charAt(i)));
        i++;
    }
    
    }
    public static void morseToEng(HashMap<String,Character> reversedHashMap){
    
    Scanner scan=new Scanner(System.in);
    System.out.println("Please enter the morse code that you want to translate to english: ");
    String morse=scan.nextLine().toUpperCase();
    int i=0;
    while(i<morse.length()){
        System.out.print();
        i++;
    }
   
    } 

讀取輸入后,按空格分割輸入字符串。 對於每個元素,查找字符(值)map 的摩爾斯電碼(鍵)並打印值。

    for (String singleCode : morse.split(" ")) {
        System.out.print(reversedHashMap.get(singleCode));
    }

樣品輸入 output

Welcome to the translator. Type 1 for English to Morse or type 2 for Morse to English: 
2
Please enter the morse code that you want to translate to english: 
... -- ...
SMS

您可以簡單地逐個獲取莫爾斯電碼,在每一行中獲取一個莫爾斯電碼,對其進行處理並將其連接為結果字符串,如果您想在一行中獲取多個莫爾斯電碼,請在它們之間使用分隔符,然后根據拆分字符串該拆分器例如a #。

所以你會得到 --#.--#..-- 然后將其拆分為:

for (String singleMorse : morse.split("#")) {
        System.out.print(reversedHashMap.get(singleCode));
    }

注意:因為你的摩爾斯電碼中有SPACE ,所以使用SPACE以外的拆分器更簡單

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM