简体   繁体   中英

Convert number to Roman numerals

I'm having some issues with a conversion algorithm I'm working in. The this is that it works fine unless i put a round number like 100, 200, 300, NNN... Any clue what could it be?

Method code:

 public static String decimalToRomano(int decimal) {
        int x = 0;
        int cent, dece, unid;
        cent = x / 100;
        x = decimal % 100;
        dece = x / 10;
        unid = decimal % 10;
        String romano = "";
        switch (cent) {
            case 1:
                romano = romano + "C";
                break;
            case 2:
                romano = romano + "CC";
                break;
            case 3:
                romano = romano + "CCC";
                break;
            case 4:
                romano = romano + "CCCC";
                break;
            case 5:
                romano = romano + "D";
                break;
            case 6:
                romano = romano + "DC";
                break;
            case 7:
                romano = romano + "DCC";
                break;
            case 8:
                romano = romano + "DCCC";
                break;
            case 9:
                romano = romano + "DCCCC";
                break;
        }
            switch (dece) {
            case 1:
                romano = romano + "X";
                break;
            case 2:
                romano = romano + "XX";
                break;
            case 3:
                romano = romano + "XXX";
                break;
            case 4:
                romano = romano + "XXXX";
                break;
            case 5:
                romano = romano + "L";
                break;
            case 6:
                romano = romano + "LX";
                break;
            case 7:
                romano = romano + "LXX";
                break;
            case 8:
                romano = romano + "LXXX";
                break;
            case 9:
                romano = romano + "LXXXX";
                break;
        }
        switch (unid) {
            case 1:
                romano = romano + "I";
                break;
            case 2:
                romano = romano + "II";
                break;
            case 3:
                romano = romano + "III";
                break;
            case 4:
                romano = romano + "IIII";
                break;
            case 5:
                romano = romano + "V";
                break;
            case 6:
                romano = romano + "VI";
                break;
            case 7:
                romano = romano + "VII";
                break;
            case 8:
                romano = romano + "VIII";
                break;
            case 9:
                romano = romano + "VIIII";
                break;
        }
        return romano;
    } 

You are dividing x ( == 0 ) by 100 and 10 for your cent and decent values.

You probably want to divide decimal instead.

Also, I would provide a 0 case for your switches .

You should change:

cent = x / 100;

to

cent = decimal / 100;

1) your switch (cent) { switch nothing because:

int x = 0;
cent = x / 100;

x == 0 so always cent == 0

2) x = decimal % 100; and unid = decimal % 10; can return 0 and there is no case(0)

public static String toRoman(int value) {
    String[] singles = new String[] { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
    String[] tens = new String[] { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" };
    String[] hundreds = new String[] { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" };
    String[] thousands = new String[] { "", "M", "MM", "MMM" };

    return thousands[value / 1000] + hundreds[value % 1000 / 100] + tens[value % 100 / 10] + singles[value % 10];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM