-3

Hi I want to end loop when q was input.

So I made a code below

        System.out.println("press q to end the loop");
        String endLoop = "";
        while(!endLoop.equals("q")) {
            endLoop = sc.next();
            System.out.println(num);
            num++;
            Thread.sleep(1000);
        }

The code I made have some problem. My code wait for user to input every time. But I don't want to wait for input every repeat. I just want to catch when user input q by keyboard. And then I want to end the loop.

What should I do to solve this problem??

codingRookie
  • 109
  • 7

1 Answers1

-1

You can take a flag, initializing with true, and after the first input is taken, change it to false.