Why doesn't my “While Loop” print the computation of finding the average “score”?

后端 未结 5 1027
臣服心动
臣服心动 2021-01-25 19:10

I am writing a program that reads a sequence of positive integers input by the user. User will only enter one integer at a time.Then it will compute the average of those integer

5条回答
  •  情深已故
    2021-01-25 19:44

    The problem is that the input.nextInt() should be part of the loop. The way you wrote it, the code gooes into an infinite loop whenever the first input is non-zero. Instead, do:

    while ((integer = input.nextInt())  != 0) {
      count = count + 1;  
      sum = sum + integer; 
      average = sum / count;
    }
    

提交回复
热议问题