Comparison of if-Else not continuing even with Else in Java

Asked

Viewed 194 times

0

I have two variables:

int trabalhadorFormal;
int dispensadoIndireta;

And I’m wearing them JOptionPane.showConfirmDialog to confirm as a kind of Boolean, yes or no, for the user.

trabalhadorFormal = JOptionPane.showConfirmDialog(null, 
    "É trabalhador formal?",
    "Por favor selecione",
    JOptionPane.YES_NO_OPTION);

dispensadoIndireta = JOptionPane.showConfirmDialog(null, 
    "Foi dispensado indiretamente?",
    "Por favor selecione",
    JOptionPane.YES_NO_OPTION);

And with this I have the following code:

 if (trabalhadorFormal == JOptionPane.YES_OPTION) {
     if (dispensadoIndireta == JOptionPane.NO_OPTION) {
         do {
             opcao = Integer.parseInt(JOptionPane.showInputDialog("[1] - É empregado doméstico" + 
                 "\n[2] - Não é empregado doméstico"));

             switch (opcao) {
                 case 1:
                     JOptionPane.showMessageDialog(null, "É empregado doméstico!");
                     break;
                 case 2:
                     JOptionPane.showMessageDialog(null, "Não é empregado doméstico!");
                     break;
                 default:
                     JOptionPane.showMessageDialog(null, "Opção inválida!");
                     break;
             }
         } while (opcao != 1 && opcao != 2);           
     } else {
         JOptionPane.showMessageDialog(null, "Teste!");
     }
 }

The only problem is that when I select the options that will lead me to the else after the second if, nothing happens and the program closes me by returning successfully, as simply ignoring the else.

What is going wrong here? In building the if-else?

  • What the Bugger says about the value of dispensadoIndireta?

  • 1

    I replicated your code here and it worked right, according to your code if you choose not it enters your do while, and while if you indicate a valid condition it breaks and exits the loop. In the second if you give a yes it goes to the Else and prints test.

1 answer

1


When you enter the no-dispensation option Direct on the second if it exits the Else condition, then Else will not be processed, look at this example:

public static void main(String[] args) {
        boolean condicao = true;
        int opcao = 2;

        if (condicao == true) {
            switch (opcao) {
            case 1:
                System.out.println("Opcao 1");
                break;
            case 2:
                System.out.println("Opcao 2");
                break;
            case 3:
                System.out.println("Opcao 3");
                break;
            }
        } else {
            System.out.println("Para condicao false entra aqui!");
        }
    }

In the above case prints Option 2 and does nothing else as it sets true for condition and the if receives true then the Else will no longer be called, ie the condition has already been executed.

Browser other questions tagged

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