Convert Roman Numbers [duplicate]

0

I know I've already asked a question on the same topic, but another question has arisen. I'm developing an application that converts integers to Roman numbers, as you can see in the code below ... But, jTextField1 IS RETURNING OTHER NUMBERS, as you can see in the image, does anyone have any idea what it can be?

    private void btnConverterActionPerformed(java.awt.event.ActionEvent evt) {                                             
        int[] vaNum = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};

        String[] vaRom = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};

        int numero = Integer.parseInt(jTextField2.getText());
        System.out.printf("%-4d ", numero);
        int i = 0;
        while (numero > 0) {
            if (numero >= vaNum[i]) {
                jTextField1.setText(vaRom[i]);
                numero -= vaNum[i];
            } else {
                i++;
            }
        }
    }  
    
asked by anonymous 20.10.2017 / 04:33

1 answer

0
private void btnConverterActionPerformed(java.awt.event.ActionEvent evt) {                                             
    int[] vaNum = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};

    String[] vaRom = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};

    public String resposta;

    int numero = Integer.parseInt(jTextField2.getText());
    System.out.printf("%-4d ", numero);
    int i = 0;
    while (numero > 0) {
        if (numero >= vaNum[i]) {
            resposta += vaRom[i];
            numero -= vaNum[i];
        } else {
            i++;
        }
    }

    jTextField1.setText(resposta);
}
    
20.10.2017 / 05:00