Problem with do while

Asked

Viewed 43 times

-3

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    BancoConta p1 = new BancoConta();
    p1.inicio();

}

public void inicio() {
    do {
        System.out.println("Para criar conta bancária: (1)");
        System.out.println("Para visualizar conta: (2)");
        escolha = sc.nextInt();
        System.out.println(escolha);
    } while (escolha!=1||escolha!=2);
    System.out.println("saiu");
}

Is not printing the "came out"

1 answer

4


Before the variable escolha does not appear declared in this code.

But the big problem is that you used || instead of &&. Look at this:

escolha!=1||escolha!=2

Think about it, if escolha for 1, we have that she is different from 2, so continue on do-while. If it is 2, then it is different from 1 and also continues.

What you wanted was this:

escolha != 1 && escolha != 2

Browser other questions tagged

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