Java - How to break out of while with hasNext() condition?

前端 未结 5 1189
被撕碎了的回忆
被撕碎了的回忆 2021-01-24 04:11

I am writing a simple program to calculate the average of a set of numbers. You get the numbers using Scanner, so I am using while loop with .has

5条回答
  •  时光说笑
    2021-01-24 04:59

    The problem is that Scanner will always expect an integer from System.in. You could break out of the loop using a sentinal value e.g. -1:

    if (temp == -1) {
       break;
    }
    

提交回复
热议问题