gpt4 book ai didi

java - 根据 HashMap 检查字符串

转载 作者:塔克拉玛干 更新时间:2023-11-01 23:09:11 24 4
gpt4 key购买 nike

我想构建一个将英语翻译成摩尔斯电码的程序,反之亦然,我决定使用散列映射来执行此操作,但我不确定如何通过散列映射运行字符串并获得翻译在最后。这是我目前的代码:

import java.util.HashMap;
import java.util.Map;

public class MorseCodeTranslator{

public static String translateToMorseCode() {
String englishtoMorse = "";
String translation = null;

Map<Character, String> morse = new HashMap<Character, String>();
morse.put('a', "._");
morse.put('b', "_...");
morse.put('c', "_._");
morse.put('d', "_..");
morse.put('e', ".");
morse.put('f', ".._.");
morse.put('g', "__.");
morse.put('h', "....");
morse.put('i', "..");
morse.put('j', ".___");
morse.put('k', "_.");
morse.put('l', "._..");
morse.put('m', "__");
morse.put('n', "_.");
morse.put('o', "___");
morse.put('p', ".__.");
morse.put('q', "__._");
morse.put('r', "._.");
morse.put('s', "...");
morse.put('t', "_");
morse.put('u', ".._");
morse.put('v', "..._");
morse.put('w', ".__");
morse.put('x', "_.._");
morse.put('y', "_.__");
morse.put('z', "__..");

return translation;
}

public static String translateFromMorseCode() {
String morsetoEnglish = "";
String translation = null;

Map<Character, String> morse = new HashMap<Character, String>();
morse.put('a', "._");
morse.put('b', "_...");
morse.put('c', "_._");
morse.put('d', "_..");
morse.put('e', ".");
morse.put('f', ".._.");
morse.put('g', "__.");
morse.put('h', "....");
morse.put('i', "..");
morse.put('j', ".___");
morse.put('k', "_.");
morse.put('l', "._..");
morse.put('m', "__");
morse.put('n', "_.");
morse.put('o', "___");
morse.put('p', ".__.");
morse.put('q', "__._");
morse.put('r', "._.");
morse.put('s', "...");
morse.put('t', "_");
morse.put('u', ".._");
morse.put('v', "..._");
morse.put('w', ".__");
morse.put('x', "_.._");
morse.put('y', "_.__");
morse.put('z', "__..");

return translation;
}
}

我希望能够通过 HashMap 运行 englishtoMorsemorsetoEnglish 中的任何内容,然后将字符转换为它们在 HashMap 中关联的值在 translation 中输出它们。

最佳答案

将 map 创建为静态字段。添加莫尔斯字母表。然后创建一个方法它将要翻译的文本作为参数。然后迭代文本以翻译每个字符并使用翻译后的字符创建将返回的字符串。

我已经用你的代码和我的解释做了一个例子。该代码仅适用于莫尔斯英文文本。您必须添加其他方向。

import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone
{

private static final Map<Character, String> morse = new HashMap<Character, String>();
static {
morse.put('a', "._");
morse.put('b', "_...");
morse.put('c', "_._");
morse.put('d', "_..");
morse.put('e', ".");
morse.put('f', ".._.");
morse.put('g', "__.");
morse.put('h', "....");
morse.put('i', "..");
morse.put('j', ".___");
morse.put('k', "_.");
morse.put('l', "._..");
morse.put('m', "__");
morse.put('n', "_.");
morse.put('o', "___");
morse.put('p', ".__.");
morse.put('q', "__._");
morse.put('r', "._.");
morse.put('s', "...");
morse.put('t', "_");
morse.put('u', ".._");
morse.put('v', "..._");
morse.put('w', ".__");
morse.put('x', "_.._");
morse.put('y', "_.__");
morse.put('z', "__..");
morse.put(' ', " ");
}
public static void main (String[] args) throws java.lang.Exception
{
String str = "Hello World";
System.out.println(translate(str));
}



public static String translate(String text) {
StringBuilder builder = new StringBuilder();
String lower = text.toLowerCase();
for (int i = 0; i < text.length(); i++) {
builder.append(morse.get(lower.charAt(i)));
}
return builder.toString();
}

}

输出:

......_..._..___ ._____._.._.._..

工作示例:

http://ideone.com/uWGAtU

关于java - 根据 HashMap 检查字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33827442/

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