如何计算两个hashmap值?

fjnneemd  于 2021-08-20  发布在  Java
关注(0)|答案(3)|浏览(203)

我的教授给了我们这个项目,你需要把用户输入的罗马数字转换成数字的文本形式。这对我来说有点难以承受,因为我还是java的初学者。
这是我制作的hashmap(ik这不是最好的,也没有太多的逻辑),我想知道如果用户输入像“dviii”,其中“d”的值为“五百”,而“viii”的值为“八”,那么如何计算hashmap值,然后将其打印为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));
    }
}
0lvr5msh

0lvr5msh1#

我建议将罗马数字转换为十进制数字(这里是一个示例代码),将罗马数字转换为整数,然后将数字转换为单词(这里是示例代码),将数字转换为单词
你也可以这样做,但我发现这种方式非常糟糕,因为它通过制作Huuuuge hashmap和一堆不成功的代码和重复来浪费内存

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

whhtz7ly2#

您可以首先将罗马输入转换为十进制整数,然后将该十进制数转换为单词

hfyxw5xn

hfyxw5xn3#

首先,你的问题可以用算法来解决,正如emil所说,我认为定义静态值来解析罗马数字是不可行的。例如,在代码中,您没有指定字符串的长度以将其视为有效数字,并且无法区分不同符号排列之间的差异。
例子:
如果一次转换单个字符: DIV 会转化为 [500, 1, 5] .
因此,;您需要指定一些规则——换句话说,您需要一个算法。
因此,您有两种选择之一:
自己实现算法,这对初学者来说可能相当棘手。
此选项需要一个不同的问题,因为它与Map相关问题无关,您可以在此网站上询问/搜索关于实施 roman numerical converter 或者在这种情况下。
使用一个具有此功能的库,通过一个小搜索我找到了这一个

相关问题