Issue
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;
}
Solution
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
.
Answered By - Mena