如何连接两个hashmap值



我的教授给了我们这个项目,您需要将用户输入中的罗马数字转换为数字的文本形式。这对我来说有点难以承受,因为我还是Java的初学者。

这是我制作的哈希图(Ik这不是最好的,也没有太多逻辑(,我想知道如果用户输入像"DVIII"这样的"D"值为"0"的东西,你如何连接哈希图值;五百";并且"VIII"的值为"VIII";八";然后将其打印为DVIII=五百八十八

import java.util.Scanner;
import java.util.HashMap;
public class Main{
public static void main(String args[]){

HashMap<String, String> Roman = new HashMap<String, String>();
Roman.put("I","One");
Roman.put("II","Two");
Roman.put("III","Three");
Roman.put("IV","Four");
Roman.put("V","Five");
Roman.put("VI","Six");
Roman.put("VII","Seven");
Roman.put("VIII","Eight");
Roman.put("IX","Nine");
Roman.put("X","Ten");
Roman.put("XI","Eleven");
Roman.put("XII","Twelve");
Roman.put("XIII","Thirteen");
Roman.put("XIV","Fourteen");
Roman.put("XV","Fifteen");
Roman.put("XVI","Sixteen");
Roman.put("XVII","Seventeen");
Roman.put("XVIII","Eighteen");
Roman.put("XIX","Nineteen");
Roman.put("XX","Twenty");
Roman.put("XXX","Thirty");
Roman.put("XL","Fourty");
Roman.put("L","Fifty");
Roman.put("LX","Sixty");
Roman.put("LXX","Seventy");
Roman.put("LXXX","Eighty");
Roman.put("XC","Ninety");
Roman.put("C","One Hundred");
Roman.put("CC","Two Hundred");
Roman.put("CCC","Three Hundred");
Roman.put("CD","Four Hundred");
Roman.put("D","Five Hundred");
Roman.put("DC","Six Hundred");
Roman.put("DCC","Seven Hundred");
Roman.put("DCCC","Eight Hundred");
Roman.put("CM","Nine Hundred");
Roman.put("M","One Thousand");
Roman.put("MM","Two Thousand");
Roman.put("MMM","Three Thousand");
Roman.put("MMMM","Four Thousand");

Scanner Input = new Scanner(System.in);
System.out.print("nEnter a Roman Numeral: ");
String RomanNumeral = Input.nextLine();

System.out.print("n The equivalent of the Roman numeral "+RomanNumeral+" is " + Roman.get(RomanNumeral));
}
}

我建议将罗马数转换为十进制数(下面是一个示例代码(将罗马数字转换为整数然后将该数字转换为单词(这里是示例代码(将数字转换为单词

你也可以这样做,但我发现这种方式真的很糟糕,因为它通过制作一个huuuuuge哈希图和一堆不可接受的代码和重复来浪费内存

import java.util.Map;
import java.util.Scanner;
import java.util.HashMap;
public class Main{
public static void main(String args[]){
//all numbers one to nine
HashMap<String, String> RomanFirstDigit = new HashMap<String, String>();
//all numbers ten to twenty
HashMap<String, String> RomanToTwenty = new HashMap<String, String>();
//twenty, thirty, forty etc
HashMap<String, String> RomanSecondDigit = new HashMap<String, String>();
RomanFirstDigit.put("I","One");
RomanFirstDigit.put("II","Two");
RomanFirstDigit.put("III","Three");
RomanFirstDigit.put("IV","Four");
RomanFirstDigit.put("V","Five");
RomanFirstDigit.put("VI","Six");
RomanFirstDigit.put("VII","Seven");
RomanFirstDigit.put("VIII","Eight");
RomanFirstDigit.put("IX","Nine");
RomanToTwenty.put("X","Ten");
RomanToTwenty.put("XI","Eleven");
RomanToTwenty.put("XII","Twelve");
RomanToTwenty.put("XIII","Thirteen");
RomanToTwenty.put("XIV","Fourteen");
RomanToTwenty.put("XV","Fifteen");
RomanToTwenty.put("XVI","Sixteen");
RomanToTwenty.put("XVII","Seventeen");
RomanToTwenty.put("XVIII","Eighteen");
RomanToTwenty.put("XIX","Nineteen");
RomanSecondDigit.put("XX","Twenty");
RomanSecondDigit.put("XXX","Thirty");
RomanSecondDigit.put("XL","Fourty");
RomanSecondDigit.put("L","Fifty");
RomanSecondDigit.put("LX","Sixty");
RomanSecondDigit.put("LXX","Seventy");
RomanSecondDigit.put("LXXX","Eighty");
RomanSecondDigit.put("XC","Ninety");
//make a hashmap with all numbers between one and hundred
HashMap<String, String> RomanOneToHundred = new HashMap<String, String>();

RomanOneToHundred.putAll(RomanFirstDigit);
RomanOneToHundred.putAll(RomanSecondDigit);
RomanOneToHundred.putAll(RomanToTwenty);
//loop over the hashmap with twenty, thirty etc with the one that has one to nine
for (Map.Entry<String,String> entryFirst : RomanSecondDigit.entrySet()){
for (Map.Entry<String,String> entrySecond : RomanFirstDigit.entrySet()){
RomanOneToHundred.put(entryFirst.getKey() + entrySecond.getKey(),
entryFirst.getValue() + " " + entrySecond.getValue());
}
}
Scanner Input = new Scanner(System.in);
System.out.print("nEnter a Roman Numeral: ");
String RomanNumeral = Input.nextLine();
System.out.print("n The equivalent of the Roman numeral "+RomanNumeral+" is " + RomanOneToHundred
.get(RomanNumeral));
}
}

好的,首先,你的问题可以用算法来解决,正如Emil所说,我认为定义静态值来解析罗马数字是不可行的。例如,在代码中,您没有指定字符串的长度以将其视为有效数字,并且无法区分不同符号排列之间的差异。

示例:

如果一次转换一个字符:DIV将转换为[500, 1, 5]

因此;您将需要指定一些规则-换句话说,您将需要一个算法

所以,你有两种选择之一:

  1. 自己实现算法,这对初学者来说可能相当棘手

此选项需要一个不同的问题,因为它与映射连接无关。您可以在该网站上询问/搜索有关在该上下文中实现roman numerical converter或其他内容的问题。

  1. 使用具有此功能的库,通过小搜索我找到了这个

最新更新