【问题标题】:how to change an integer value to a roman numeral [duplicate]如何将整数值更改为罗马数字[重复]
【发布时间】:2013-11-06 13:55:35
【问题描述】:

计算完两个数字后,如何将整数更改为罗马数字。我不知道如何将整数值更改为等效的罗马数字值。

import java.util.Scanner;
/**
 * Asks the user to input two numbers along with the "+" sign in roman numerals to  find the sum of the numbers.
 *  
 * @author (Harpreet Singh)
 * Brampton Centennial Secondary School
 * @version (05/11/2013)
 */
public class RomanCalculator
{
    public static int total = 0;
    public static void main(String args[]) 
    {
        Scanner stdIn = new Scanner(System.in);
        System.out.print("Enter a Roman Number:> ");
    char[] roman = stdIn.nextLine().toCharArray();

    for(int i = roman.length-1; i > -1; i--)
    {
        switch(roman[i])
        {
            case 'I':
            total += value(roman[i]); break;
            case 'V':
            case 'X':
            case 'L':
            case 'C':
            case 'D':
            case 'M':
            if(i != 0 && (value(roman[i-1]) < value(roman[i])))
            {
                total += value(roman[i]) - value(roman[i-1]);
                i--;
            }
            else
            {
                total += value(roman[i]);
            }
            break;
        }
    }        

    if (total>1000)
    {
        System.out.println(total);
        System.out.println("ERROR ILLEGAL ENTRY!");
        System.exit(0);
    }
    System.out.println("The Total Is: "+total);
}

public static int value(char c)
{
    switch(c)
    {
        case 'I':
        return 1;
        case 'V':
        return 5;
        case 'X':
        return 10;
        case 'L':
        return 50;
        case 'C':
        return 100;
        case 'D':
        return 500;
        case 'M':
        return 1000;
        default:
        return ' ';
    }
}
}

【问题讨论】:

  • 你知道你有什么单位 - 这与将时间以秒为单位转换为以小时/分钟/秒为单位的时间完全相同。
  • this 是我在网上找到的一个类。可能是您正在寻找的。​​span>

标签: java


【解决方案1】:

Source 这个有用的方法:

public static String IntegerToRomanNumeral(int input)
{
    if (input < 1 || input > 3999)
        throw new InvalidInputException();

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

【讨论】:

  • 如何在我上面写的程序中使用这段代码
  • 删除您的代码,并使用您要转换的int 调用此方法,如下所示:IntegerToRomanNumeral(yourInt);
  • 表示要删除方法 int value(char c)
  • @helpforICS 不,你从 int 转换为 roman 并改用我的那个。
  • 我还是不明白,我有点糊涂
【解决方案2】:
public static int convertRomanNumeral(String roman) {
    String modRoman = roman.replace("CM", "DCCCC");
    modRoman = modRoman.replace("CD", "CCCC");
    modRoman = modRoman.replace("XC", "LXXXX");
    modRoman = modRoman.replace("XL", "XXXX");
    modRoman = modRoman.replace("IX", "VIIII");
    modRoman = modRoman.replace("IV", "IIII");

    Map<Character, Integer> symbols = new HashMap<>();
    symbols.put('M', 1000);
    symbols.put('D', 500);
    symbols.put('C', 100);
    symbols.put('L', 50);
    symbols.put('X', 10);
    symbols.put('V', 5);
    symbols.put('I', 1);

    char[] chars = modRoman.toCharArray();
    int total = 0;
    for (char c : chars) {
        total += symbols.get(c);
    }

    return total;
}

如果您要经常调用它,我会将 symbols 的初始化移到它自己的方法中,但是将它们全部放在一个方法中可以更简单地在此处发布(恕我直言)。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-06-26
  • 1970-01-01
  • 2019-01-09
  • 2011-10-25
  • 2012-10-09
  • 1970-01-01
相关资源
最近更新 更多