import java.util.Scanner; public class Test { public static void act() { String phrase; Scanner sc = new Scanner(System.in); System.out.println("Да или нет?"); String ans[] = {"да", "нет"}; phrase = sc.next(); phrase = phrase.toLowerCase(); //конвертируем всю строку в строчные буквы boolean a = phrase.equals(ans[0]); boolean b = phrase.equals(ans[1]); if (a || b) { System.out.println("На что ты ответил то?!"); } else { while (!a || !b) { System.out.println("Я просто просил ответить 'да' или 'нет', не более!"); phrase = sc.next(); a = phrase.equals(ans[0]); b = phrase.equals(ans[1]); System.out.println(a); System.out.println(b); } } } } 

Why, if it comes to a cycle, it runs forever. Help me fix it. It is not clear why the value of a and b does not change to false .

  • Use while(sc.hasNext()) {...} - And

1 answer 1

When you enter "yes" or "no" one of these options will be positive, and the second negative.

 a = phrase.equals(ans[0]); b = phrase.equals(ans[1]); 

And condition

 !a || !b 

there will always be truth. Do a check on one condition, not on two.

 if(phrase.equals(ans[0])({ a = true; }else{ a= false; }; 

Something like this