gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:49:55 26 4
gpt4 key购买 nike

我正在尝试学习 Java 中的 HashMap 和二维数组。我们有一个作业是因为有一个扫描器可以接受一个字符串并将其转换为摩尔斯电码。我们正在使用的代码基于一个充满 if 语句的方法,但我想了解如何使用列表、散列图或二维数组来做这样的事情。我的代码如下:

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;
}

}

我这样做完全是出于好奇。我已经意识到这样的冗余是一个巨大的禁忌。提前致谢!

最佳答案

更简单的方法是使用一个初始化程序,使用字母作为键将字符加载到 HashMap 中。然后,当您循环输入字符串中的字符时,您只需执行

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

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 语句。

关于java - 使用 Java 进行摩尔斯电码转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15101912/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com