Why is first for loop iteration skipped?

后端 未结 5 2202
粉色の甜心
粉色の甜心 2021-01-26 10:57

my code is yielding an unexpected result. It seems my for loop skips the first iteration and I don\'t understand why.

public static void main(String[] args) {

         


        
5条回答
  •  长发绾君心
    2021-01-26 11:19

    Currently your call to Scanner#nextInt is not consuming the newline character so it is being passed through to your first call of Scanner#nextLine, therefore it does not block.

    You will need to add

    get.nextLine();
    

    after calling nextInt so that your first call to nextLine will block for IO.

提交回复
热议问题