1

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

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

2 回答 2

1

这个有用方法的来源:

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;
}
于 2013-11-06T14:03:25.770 回答
0
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到它自己的方法中,但是将它们全部保存在一个方法中使得在此处发布变得更简单(恕我直言)。

于 2013-11-06T14:15:43.067 回答