Convert Roman Numerals

Asked

Viewed 1,100 times

0

I know I’ve asked a question on the same subject, but another question has arisen. I am developing an application that converts whole numbers 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 might be? inserir a descrição da imagem aqui

    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++;
            }
        }
    }  

  • What is the relationship between the latter and the latter: https://answall.com/q/247563/64969?

  • It’s about the same program

  • I did not understand the difference between the questions, the titles are very similar. What is the difference between the questions? If the other question is related and important, it is willingly to leave it linked together with a text of yours explaining how they conserve

  • Example: https://answall.com/q/245213/64969

  • In the other the program simply did not work, in this there is only one error ... as quoted above

  • You know that the setText replaces the previous text, right? So all this iteration is being discarded at the expense of the last operation performed. Why not return to String for this result set the text? And anyway, your text is confusing me

  • Friend, I don’t understand what you mean, I’m just starting in the Java world, can you be clearer please? How to return to String?

  • https://answall.com/a/235774/64969

Show 3 more comments

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);
}
  • You can make it clearer where to put and what to delete in the code?

  • Create the Reset variable before the while, swap your while for the while then put my setText after the while

  • Dude, the error is in System.ou.printf("%-4d", number);

  • The code you gave me is full of mistakes

  • Is the error in 20 or X? I edited the answer after and I entered the smaller code either the old or the new?

  • The error is in X, was to appear XX ...

  • The printf is used to write in the output the value of the variable number not? And the image is working correctly. What’s the problem with it?

  • That’s right, you’re right ... but the exit is wrong, 20 in Roman numerals is XX ... I have no idea where the error is ...

  • I guess the mistake is in jTextField1.setText(vaRom[i]); since when he first passes arrow X on the textfield and the second time he again arrow X on writing instead of putting another X, so I created a variable answer and added the variable answer X with += no over writing and then set the textfield with the variable respoata

  • So this could be it ... But I would just like you to send me the entire code so I understand what you’re trying to show me, from the creation part of the arrays ...

  • I edited by placing the complete code of the button

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.