How to repeat something until correct input is given in java?

后端 未结 2 1577
野趣味
野趣味 2021-01-26 00:58

I am trying to make a application and for one part of the application I need to get a input from the user stating how many times there click their mouse in 1 second. I want the

2条回答
  •  孤街浪徒
    2021-01-26 01:23

    Use do while loop instead of while loop. This lets you run the loop atleast once before it tests the condition, where you can test the input.

    String input = null;
    do {
        //get the input from user
    } while (isInputValid); //check input validity here
    

    Example:

     Scanner input= new Scanner(System.in);
     do {
        System.out.println("Please enter the advertising cost: ");
        advertCost = input.nextDouble();
    
        } while (advertCost >= 100000 || advertCost <= 900000);
    

    I have added link on how to validate inputs

    Hope this helps!

提交回复
热议问题