Why is first for loop iteration skipped?

后端 未结 5 2155
粉色の甜心
粉色の甜心 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条回答
  •  闹比i
    闹比i (楼主)
    2021-01-26 11:22

    Notice that blank line between 4 and 1?

    4
           //this is it
    1
    

    When you call get.NextInt() you'd not eating the whole line, just the next int. The empty rest of the line is eaten in your first loop iteration. Add a call to get.nextLine() after reading the int.

提交回复
热议问题