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

后端 未结 5 1059
臣服心动
臣服心动 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

    This is because you are never actually summing over more than one integer. The user only ever enters one number. As a result your loop is essentially acting on just the one number. You need to put the input inside the while loop and save a running sum and count there. Something more like this

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

提交回复
热议问题