使用Java进行摩尔斯电码转换

我正在尝试学习Java中的哈希映射和2D数组.我们有一个任务,因为有一个扫描仪接受一个字符串并将其转换为莫尔斯电码.我们使用的代码基于一个充满if语句的方法,但我想学习如何使用list,hashmaps或2D数组做这样的事情.我的代码如下:

import java.util.*;

public class MorseConversion 
{


public static void main(String[] args) 
{

    Scanner userInput = new Scanner(System.in); 
    System.out.println("Please enter a phrase to convert to morse code: ");
    String userString = userInput.nextLine();
    System.out.println("");
    System.out.println(stringConvert(userString));
}


public static String stringConvert(String userString)
{
    String currentChar;
    String getMorseChar;
    String convertedString = "";

    for (int i = 0; i < userString.length(); i++)
    {
        //Get character at i position
        currentChar = userString.charAt(i) + "";  

        //convert character to morse code
        getMorseChar = convert(currentChar);

        //seperate words with the | symbol
        if (getMorseChar.equals(" "))
        {
            convertedString = convertedString + "  |  ";
        } 

        else 
        {
            //concat the converted letter
            convertedString = convertedString + getMorseChar;

            //Add a space between each letter
            if (!getMorseChar.equals(" ")) 
            {
                convertedString = convertedString + " ";
            }   
        }           
    }

    return convertedString;

}
 public static String convert (String toEncode)  
    {  
        String morse = toEncode;  

        if (toEncode.equalsIgnoreCase("a"))  
            morse = ".-";  
        if (toEncode.equalsIgnoreCase("b"))  
            morse = "-...";  
        if (toEncode.equalsIgnoreCase("c"))  
            morse = "-.-.";  
        if (toEncode.equalsIgnoreCase("d"))  
            morse = "-..";  
        if (toEncode.equalsIgnoreCase("e"))  
            morse = ".";  
        if (toEncode.equalsIgnoreCase("f"))  
            morse = "..-.";  
        if (toEncode.equalsIgnoreCase("g"))  
            morse = "--.";  
        if (toEncode.equalsIgnoreCase("h"))  
            morse = "....";  
        if (toEncode.equalsIgnoreCase("i"))  
            morse = "..";  
        if (toEncode.equalsIgnoreCase("j"))  
            morse = ".---";  
        if (toEncode.equalsIgnoreCase("k"))  
            morse = "-.-";  
        if (toEncode.equalsIgnoreCase("l"))  
            morse = ".-..";  
        if (toEncode.equalsIgnoreCase("m"))  
            morse = "--";  
        if (toEncode.equalsIgnoreCase("n"))  
            morse = "-.";  
        if (toEncode.equalsIgnoreCase("o"))  
            morse = "---";  
        if (toEncode.equalsIgnoreCase("p"))  
            morse = ".--.";  
        if (toEncode.equalsIgnoreCase("q"))  
            morse = "--.-";  
        if (toEncode.equalsIgnoreCase("r"))  
            morse = ".-.";  
        if (toEncode.equalsIgnoreCase("s"))  
            morse = "...";  
        if (toEncode.equalsIgnoreCase("t"))  
            morse = "-";  
        if (toEncode.equalsIgnoreCase("u"))  
            morse = "..-";  
        if (toEncode.equalsIgnoreCase("v"))  
            morse = "...-";  
        if (toEncode.equalsIgnoreCase("w"))  
            morse = ".--";  
        if (toEncode.equalsIgnoreCase("x"))  
            morse = "-..-";  
        if (toEncode.equalsIgnoreCase("y"))  
            morse = "-.--";  
        if (toEncode.equalsIgnoreCase("z"))  
            morse = "--..";  
        if (toEncode.equalsIgnoreCase("0"))  
            morse = "-----";  
        if (toEncode.equalsIgnoreCase("1"))  
            morse = ".----";  
        if (toEncode.equalsIgnoreCase("2"))  
            morse = "..---";  
        if (toEncode.equalsIgnoreCase("3"))  
            morse = "...--";  
        if (toEncode.equalsIgnoreCase("4"))  
            morse = "....-";  
        if (toEncode.equalsIgnoreCase("5"))  
            morse = ".....";  
        if (toEncode.equalsIgnoreCase("6"))  
            morse = "-....";  
        if (toEncode.equalsIgnoreCase("7"))  
            morse = "--...";  
        if (toEncode.equalsIgnoreCase("8"))  
            morse = "---..";  
        if (toEncode.equalsIgnoreCase("9"))  
            morse = "----.";  
        if (toEncode.equalsIgnoreCase("."))  
            morse = ".-.-";  
        if (toEncode.equalsIgnoreCase(","))  
            morse = "--..--";  
        if (toEncode.equalsIgnoreCase("?"))  
            morse = "..--..";  

        return morse;  
    }  

 }

我出于好奇而严格地这样做.我已经把它砸到了我的脑海里,像这样的冗余是一个巨大的禁忌.提前致谢!

解决方法:

更简单的方法是使用初始化程序,使用字母作为键将字符加载到哈希映射中.然后,当您循环输入字符串中的字符时,您只需执行操作即可

在课程开始时做这样的事情:

private static HashMap<String, String> codes = new HashMap<String, String>();
    static{
        codes.put("a", ".-");
        codes.put("b", "-...");
        bla bla bla
    }

然后在你的循环中

//convert character to morse code
        getMorseChar = convert(currentChar);

你将会拥有

getMorseChar = code.get(currentChar.toLowerCase());

没有更讨厌的elseif声明.

上一篇:Python编解码器错误除外?


下一篇:php json_encode()函数返回对象和数组问题