Why won't the program include the first input from the user in the calculation of the average?

This code is meant to output an average of -1 if the first value entered by the user is -1. If the value entered is not -1, then it will calculate the average of the given values, and it will stop when the value -1 is entered and it will be the stopping signal for the program. The task is to modify an existing DoWhile loop - which I have done.

Now, after having written the code, I can't seem to understand why the program isn't including the first input from the user, for the calculation of the average.

Scanner input = new Scanner(System.in);

float grade;
float sumGrades, average;
int studentCount = 0;
sumGrades = 0;

final int STOP = -1;

System.out.printf("\nEnter a grade (%d to finish): ", STOP);
grade = input.nextFloat();
if(grade == STOP)
{
      sumGrades = sumGrades + grade;
      studentCount++;
}
else
    do
    {
    System.out.printf("\nEnter a grade (%d to finish): ", STOP);
    grade = input.nextFloat();

    if(grade != STOP)
      {
      sumGrades = sumGrades + grade;
      studentCount++;
      }
    } while(grade != STOP);

average = sumGrades / studentCount;
System.out.printf("The average obtained is %.1f\n", average);

OUTPUT:

Enter a grade (-1 to finish): 2

Enter a grade (-1 to finish): 3

Enter a grade (-1 to finish): 4

Enter a grade (-1 to finish): 5

Enter a grade (-1 to finish): 2

Enter a grade (-1 to finish): 3

Enter a grade (-1 to finish): -1

The average obtained is 3.4

OUTPUT 2:

Enter a grade (-1 to finish): 2

Enter a grade (-1 to finish): -1

The average obtained is NaN

Image of Compiled Code and Run

Code

2 answers

  • answered 2018-11-08 08:32 Stephen C

    Look carefully at what you have written.

    System.out.printf("\nEnter a grade (%d to finish): ", STOP);
    grade = input.nextFloat();
    

    OK so far

    if(grade == STOP)
    {
          sumGrades = sumGrades + grade;
          studentCount++;
    }
    

    So ... if the grade means "stop" ... you add it to the sum, and count?!?

    else
        do
        {
    

    But if the previous grade was NOT a stop ... you throw it away and read the next grade!?!

        System.out.printf("\nEnter a grade (%d to finish): ", STOP);
        grade = input.nextFloat();
    

    See the problem?

  • answered 2018-11-08 08:33 Raso

    main issiue is here:

       final int STOP = -1;
    
    
            System.out.printf("\nEnter a grade (%d to finish): ", STOP);
          grade = input.nextFloat(); 
    if(grade == STOP)
    {
          sumGrades = sumGrades + grade;
          studentCount++;
    }
    

    here you read input, and just check if its STOP, bud you do nothing with input if isnt STOP in next

    else
        do
        {
        System.out.printf("\nEnter a grade (%d to finish): ", STOP);
        grade = input.nextFloat();
    

    you again read input and then process it if isnt STOP and you lose your first input

    solution by one cycle:

    do
        {
        System.out.printf("\nEnter a grade (%d to finish): ", STOP);
        grade = input.nextFloat();
            if(grade != STOP)
              {
                sumGrades = sumGrades + grade;
                studentCount++;
              }
    
          } while(grade != STOP);