//我只是一個初學者,這是我的第一個程序,它工作正常,但有什麼辦法可以讓它變得更好嗎?英文到莫爾斯碼轉換器,我怎樣才能使它更有效率?
import java.util.*;
public class NewClass1 {
public static void main(String[] args) {
Character alphabet [] = {'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', ' '};
String morseCode [] = {".- ", "-... ", "-.-. ", "-.. ", ". ", "..-. ", "--. ", ".... ", ".. ", ".--- ", "-.- ", ".-.. ", "-- ", "-. ", "--- ", ".--. ", "--.- ", ".-. ", "... ", "- ", "..- ", "...- ", ".-- ", "-..- ", "-.-- ", "--.. ", "| "};
//putting alphabets and morsecode in HashMap
Map<Character, String> morseCodes = new HashMap<>();
for(int i = 0; i < alphabet.length; i++)
{
morseCodes.put(alphabet[i], morseCode[i]);
}
//Took user input and converted it into LowerCase Character Array
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
char[] translate = input.toLowerCase().toCharArray();
//Translating user input(translate[]) using for loop
for(int j=0; j<input.length(); j++){
System.out.print(morseCodes.get(translate[j]));
}
}
}
您可以正確縮進它。 – khelwood
這不是一個特定的編程問題。 –
我相信這種類型的問題在[代碼評論](https://codereview.stackexchange.com/)堆棧交換 – hrust