简体   繁体   中英

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!

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"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM