Actualmente estoy aprendiendo .Net a través de C# y quisiera saber por qué en esta clase que elaboré para practicar el lenguaje, hay ciertas irregularidades al momento de entrar algunos números y no me toma el valor real.
Para probarlo, pueden crear una simple clase que realice un ciclo desde 1 - 30 llamando a el método toRoman() y se darán cuenta de los resultados.
Yo creo que el detalle se encuentra entre las líneas 42 y 47, ambas inclusive; pero de verdad que no entiendo por qué.
OJO: yo no quisiera que me dieran otro código que ya funcione, sino que alguien me pueda dar alguna pista de qué debo revisar; y así seguir aprendiendo.
Muchísimas gracias de antemano.
Código:
public class Roman { private char[][] unit; public Roman() { // // //TODO: Add constructor logic here // this.unit = new char[2][]; this.unit[0] = new char[] { 'I', 'X', 'C', 'M' }; this.unit[1] = new char[] { 'V', 'L', 'D' }; } public String toRoman(String number) { int num = int.Parse(number); return this.romanize(num, 0); } private String romanize(int number, int x) { String res=""; if (number > 0) { if ((number / 10) > 0) { res += romanize(number / 10, x+1); double aux = (double)number / 10; number /= 10; aux -= number; aux *= 10; number = (int)aux; } while (number > 0) { if (number == 9) { res += (this.unit[0][x].ToString() + this.unit[0][x + 1].ToString()); number -= 9; } if (number >= 5) { res += this.unit[1][x].ToString(); number -= 5; } if (number == 4) { res += (this.unit[0][x].ToString() + this.unit[1][x].ToString()); number -= 4; } if (number <= 3 ) { while (number > 0) { res += this.unit[0][x].ToString(); number--; } } }// end while } else { res += ""; } return res; } }