0
My program go in infinity loop and output doesn't try to convert roman to integer
I am mediator in java programing. And my roman to integer programs got an error please solve it. https://code.sololearn.com/cVA814qx5wPT/?ref=app
1 Réponse
0
how your code works
for i in each roman {'I','V','X','L','C','D','M'} and
for j in each input chars ie. {'X','V','I','I'}
compare:
'I' == 'X' false
'I' == 'V' false
'I' == 'I' true
add Arabic number 1, // [1,0,0,0]
flag=1
'I' == 'I' true
add Arabic number 1, // [1,0,0,0] (same position)
then flag=1
reset i=0
------------
next i loop i++ // i=1, roman 'V'
compare:
'V' == 'X' false
'V' == 'V' true
add Arabic number 5, // [1,5,0,0]
flag=1
'V' == 'I' false
'V' == 'I' false
then flag=1
reset i=0
next loop i++ // i=1 for roman 'V'
here starts the infinity loop because i=1 in each loop
You should interchange loops like
for (i=0; i < ch.length; i++) {
for (int j=0; j < roman.length; j++) {
...
break; //use break instead of flag