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

前端 未结 5 1328
被撕碎了的回忆
被撕碎了的回忆 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:53

    You could simply use the keyword break to stop the while loop:

    while(Input.hasNextInt()){
    
        count++;           
    
        temp = Input.nextInt();
        sum += temp;
        System.out.println(temp);
        System.out.println(count); 
        break;          
    
    } // End of while
    

提交回复
热议问题