How to get average from given values

前端 未结 3 1420
我寻月下人不归
我寻月下人不归 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:16

    You are getting the input in your loop at this line

    GPA = keyboard.nextDouble();
    

    Problem is that, it will get another input and count will be incremented by 1. So your total will be 5. You can probably make it in this way

    while (GPA >=0)
    {
        GPA = keyboard.nextDouble();
        if (GPA >=0)
        {
           total = total + GPA;
           count++;
        }
        else
           break;
    }
    

提交回复
热议问题