将整数转换为罗马数字 - Java [英] Converting Integers to Roman Numerals - Java

查看:25
本文介绍了将整数转换为罗马数字 - Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我有问题的家庭作业.

This is a homework assignment I am having trouble with.

我需要使用一种方法将整数转换为罗马数字.后来,我必须再用程序用罗马数字写出1到3999,所以硬编码就出来了.我下面的代码非常简单;这是一个基本的 I/O 循环,可以在使用我们在课堂上制作的 getIntegerFromUser 包时退出.

I need to make an integer to Roman Numeral converter using a method. Later, I must then use the program to write out 1 to 3999 in Roman numerals, so hardcoding is out. My code below is very bare-bones; it is a basic I/O loop with a way to exit while using a package for getIntegerFromUser we made in class.

有没有办法给字符串赋值,然后在调用方法时将它们相加?

Is there a way to assign values to Strings and then add them together when I call the method?

更新:我从教授那里得到了一些伪代码来帮助我,虽然我明白他想说什么,但我在 if 上遇到了一些问题s.我是否需要很多很多 if 语句,以便我的转换器正确处理罗马数字格式,或者有什么方法可以更有效地做到这一点?我已更新我的代码以反映我的占位符方法.

Update: I got some pseudo code from my professor to help me, and while I understand what he is trying to say, I am having some trouble with the ifs. Will I be needing many, many if statements so that my converter will correctly handle the Roman numeral formatting or is there a manner in which I can do this with more efficiency? I've updated my code to reflect my placeholder method.

更新(2012 年 10 月 28 日):我成功了.这是我最终使用的:

Update (Oct 28 2012): I got it working. Here's what I ended up using:

public static String IntegerToRomanNumeral(int input) {
    if (input < 1 || input > 3999)
        return "Invalid Roman Number Value";
    String s = "";
    while (input >= 1000) {
        s += "M";
        input -= 1000;        }
    while (input >= 900) {
        s += "CM";
        input -= 900;
    }
    while (input >= 500) {
        s += "D";
        input -= 500;
    }
    while (input >= 400) {
        s += "CD";
        input -= 400;
    }
    while (input >= 100) {
        s += "C";
        input -= 100;
    }
    while (input >= 90) {
        s += "XC";
        input -= 90;
    }
    while (input >= 50) {
        s += "L";
        input -= 50;
    }
    while (input >= 40) {
        s += "XL";
        input -= 40;
    }
    while (input >= 10) {
        s += "X";
        input -= 10;
    }
    while (input >= 9) {
        s += "IX";
        input -= 9;
    }
    while (input >= 5) {
        s += "V";
        input -= 5;
    }
    while (input >= 4) {
        s += "IV";
        input -= 4;
    }
    while (input >= 1) {
        s += "I";
        input -= 1;
    }    
    return s;
}

推荐答案

使用 Java 的紧凑实现 TreeMap 和递归:

A compact implementation using Java TreeMap and recursion:

import java.util.TreeMap;

public class RomanNumber {

    private final static TreeMap<Integer, String> map = new TreeMap<Integer, String>();

    static {

        map.put(1000, "M");
        map.put(900, "CM");
        map.put(500, "D");
        map.put(400, "CD");
        map.put(100, "C");
        map.put(90, "XC");
        map.put(50, "L");
        map.put(40, "XL");
        map.put(10, "X");
        map.put(9, "IX");
        map.put(5, "V");
        map.put(4, "IV");
        map.put(1, "I");

    }

    public final static String toRoman(int number) {
        int l =  map.floorKey(number);
        if ( number == l ) {
            return map.get(number);
        }
        return map.get(l) + toRoman(number-l);
    }

}

测试:

public void testRomanConversion() {

    for (int i = 1; i<= 100; i++) {
        System.out.println(i+"	 =	 "+RomanNumber.toRoman(i));
    }

}

这篇关于将整数转换为罗马数字 - Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆