How to get average from given values

前端 未结 3 1385
我寻月下人不归
我寻月下人不归 2021-01-28 15:33

How do I average?. I am suppose to find the average of the GPA, total of students, and the total of GPA. example:

input: 4

3条回答
  •  生来不讨喜
    2021-01-28 16:22

    Just swap the order of your statements in the while loop and your code should work fine. In this case,your check (whether to add the i/p to the total) will be done after you key in. Hence,when you key in -1,the you won't enter the while loop and the last value of -1 won't be added.

    while (GPA >=0) {

    total = total + GPA;
    GPA = keyboard.nextDouble();
    count++;
    

    }

提交回复
热议问题