How to interrupt a while loop

Asked

Viewed 384 times

0

I’m developing a system with Jframe from a pharmacy. It’s a college issue. In the cashier operator comes in with the quantity X of product that the customer chooses from a list of n products and then with the unit value of each product that is worth Y. At the end the system must show the total purchase value by multiplying X * Y = ?. This code is simple to do and using the terminal or I do it smoothly. An example of how I control the loop through the terminal.

while (n == 0){
        System.out.printf("\nEscolha:\n\t1 - Inserir novo produto\n\t0 - Valor Total");
            k = input.nextInt();
            if(k == 1){
                System.out.println("Quantidade de produtos: ");
                    //recebe
                System.out.println("Valor unitário");
                    //recebe
                    valorFinal = valorFinal + (quantidade * valorUnitario);
            }
            if(k == 0){
                //qualquer coisa
                n++;
                //interrompe o laço e imprime o valor
            }
    }

Using Jframe it does not work like this, because there is no way the user choose option 1 or 0. In this case I thought to create a [Add Product] button and every time it was clicked the seller would add the products and unit value and when it clicked the button [Final Value] the loop would be broken and the result would be shown.

int n = 0;
    double valorFinal = 0;

    while(n == 0){
        resposta = JOptionPane.showConfirmDialog(null, "Cadastrar nova compra?");
        if(resposta == JOptionPane.YES_OPTION){
            double qtdIten = Double.valueOf(jTextField1.getText());
            double valor = Double.valueOf(jTextField2.getText());

             valorFinal = (valorFinal + (valor * qtdIten));

        }
        else{
            jTextArea1.setText("Valor da compra: "+valorFinal);
            n++;
        }
    }

In a very amateur way I made this attempt with a Joptionpane but it did not work.

  • a simple break; must solve

  • Without a [mcve] can’t see how your screen works. So I removed the Jframe tag because there’s nothing related to this in the question.

1 answer

2


I guess what’s not working is that the JOptionPane is above the screen and you cannot return to enter new values.

Ideally you would do the following, when the user clicks on the add button you take the values (as you did inside the if same) and sum, then you assign "" to the text fields to clear the fields. Instead of the user clicking several times on the OK of the JOptionPane must click several times on the add button.

You can even have a field on the screen where you write the current final value.

Another way would be to use the JOptionPane.showInputDialog which shows a text field, so the user could insert the values of X and Y directly in the dialog. It’s not exactly professional, but it would allow you to input data. It could do this with a single command JOptionPane.showInputDialog and the values separated by space or comma, or call JOptionPane.showInputDialog twice, once for each value you want to capture.

  • Using the JOptionPane to receive the values it works. But then I would need to set the size of the loop While. I wanted to make it automatic so that the operator would keepn dice.

  • Oh yes. See, this is not the way to make a "system" just an application to practice programming ok? So in this context, you could put that if the user clicks "Cancel" (will return null) it serves to break the loop, or the user can type "end" as the first value.

  • That’s what I thought, put a message explaining. If I wanted to finish type 0, for example. Then the n that controls the while would receive 1 and the loop would be broken. Ps.: In a complex system how would this be solved? And yes it’s an activity for programming practice, I’m learning Jframe using Netbeans.

Browser other questions tagged

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