This question is an extension from the following link:
Why is my c != 'o' || c != 'x' condition always true?
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?
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;
}
}
}
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.