0

This question is an extension from the following link:

Why is my c != 'o' || c != 'x' condition always true?

enter image description here

The corresponding code is also in the link; I've edited the boolean part that caused the problem, so it takes in the correct value.

Now, the problem, as shown in the above picture, is that after the input has been captured by the java program, the loop does three more loops until it waits for the next input.

Why is it doing that? and how do I fix it?

Community
  • 1
  • 1
Jason J.Y. Kim
  • 183
  • 2
  • 12

1 Answers1

1
import java.util.Scanner;

public class test1 {

public static void main (String args[]) {

    Scanner sc = new Scanner (System.in);
    boolean game_start=false;
    char c;

while(!game_start){
        System.out.println("press o to play first");
        System.out.println("press x to play second");

         c = sc.next().charAt(0);

        System.out.println("You entered " + c);

        if(c!='o' && c!='x') 
            System.out.println("you can only enter o or x!");  
        else 
            game_start = true;   
        }
}

}

  • I tried not to use Scanner because I thought it was only for string, but I guess this way is easier. Thank you for the answer! – Jason J.Y. Kim Apr 14 '16 at 10:56