Mauricio Dziedzinski
Mauricio Dziedzinski

Reputation: 507

While loop not exiting in my code

I need my loop to end when i type "não" or "nao", but it never ends.

here's the code:

do {
        System.out.println("Digite o primeiro valor a ser trocado:");
        t1.setValor1(input.nextInt());

        System.out.println("Digite o segundo valor a ser trocado:");
        t1.setValor2(input.nextInt());

        t1.trocarValores(t1.valor1, t1.valor2);
        System.out.println(t1);
        input.nextLine();

        System.out.println("\nVoce gostaria de trocar outro número? Digite 'sim' ou 'não'.");
        parar = input.nextLine();
    }while(!"não".equalsIgnoreCase(parar) || !"nao".equalsIgnoreCase(parar));

If i cut off the "(!"não".equalsIgnoreCase(parar)" and leave only the "!"nao".equalsIgnoreCase(parar)", it works, but i have no idea why ._.

Thanks everyone!

Well, looks like the problem is with the || and the "~" in my "não", so now, how do I fix the "~"? i want it to work with the exact "não". Thanks!

Upvotes: 0

Views: 69

Answers (2)

Ninja
Ninja

Reputation: 2098

while(!("não".equalsIgnoreCase(parar) || "nao".equalsIgnoreCase(parar)) );

Upvotes: 0

M A
M A

Reputation: 72844

Change

while(!"não".equalsIgnoreCase(parar) || !"nao".equalsIgnoreCase(parar));

to

while(!"não".equalsIgnoreCase(parar) && !"nao".equalsIgnoreCase(parar));

The opposite of "não" or "nao" is not "não" and not "nao"

Upvotes: 3

Related Questions