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?

1
  • It should not print this 3 times. Can you again paste your exact code here. Commented Apr 14, 2016 at 10:39

1 Answer 1

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;   
        }
}

}

Sign up to request clarification or add additional context in comments.

1 Comment

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!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.