Continue a while loop after exception

前端 未结 6 499
春和景丽
春和景丽 2021-01-16 12:39

i have this piece of code. I wanted to return to the beginning of loop and ask for user input again. However, it always loops without stopping to ask for input. What is wron

6条回答
  •  滥情空心
    2021-01-16 13:15

    Put a line separator in your catch block.

    Scanner input = new Scanner(System.in);
    while(true)
        {
            try 
            {
                int choice = input.nextInt(); 
            } catch (InputMismatchException e)
            {
                input.next(); // Line separator
            }
    
        }
    

提交回复
热议问题