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

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

问题描述

这是我遇到问题的家庭作业。

This is a homework assignment I am having trouble with.

我需要使用方法为Roman Numeral转换器创建一个整数。之后,我必须使用该程序用罗马数字写出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?

更新:我得到了一些伪来自我教授的代码帮助我,虽然我明白他想说的是什么,但我在使用时遇到了一些麻烦,如果 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的紧凑实现< a href =http://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html =noreferrer> 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+"\t =\t "+RomanNumber.toRoman(i));
    }

}

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

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