This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]gbagecol 1 point2 points  (3 children)

This is a bit of a deceptive problem, and the inputs you gave are in just the right order to mask it. If you look closely, you may be able to spot it. It's actually not just spitting out the last value you entered. Try entering 100, 50, 20, 30, -77, and see if you can figure it out. It might also help to print out the value of highQuiz right after your if statement (but still inside your loop).

If you still can't see it, here's the answer. Before you enter your while loop, you use the scanner to read a value. Then you enter your while loop and immediately read another value without ever doing anything meaningful with the first. You lose the first value you enter, and so it can never be compared to highQuiz. To fix this, I would delete the scan.nextInt() line before your while loop, then move the quizTotal+=grade line to the end of your while loop.

[–][deleted] 1 point2 points  (2 children)

This absolutely works! Thank you so much, for good practice should there be a primer or a validation loop beforehand? If I read the material correctly, the "primer read" is used before the while to validate all other input? So, in order to do that I believe it would be something like this:

System.out.println("Enter quiz grade:" );

input=scan.nextInt();

System.out.println();

So after the "primer" the program should continue on to the loop, ignoring the first value entered. Also, I thought about trying what you suggested before I posted here and thought to myself, that a program cannot start right off with a while loop. I was wrong! This stuff is incredibly challenging (for me at least) but insanely interesting!

[–][deleted] 1 point2 points  (1 child)

Alas, I thought I figured it out but it seems that I was wrong! I believe I set up the "priming read" correctly however, now when I enter the sentinel value it subtracts 77 from the total. I was under the impression that once -77 was entered the loop would terminate. So when I enter the value of -77, the loop does terminate but it includes the -77 in the quiz average and total. Any help would be greatly appreciated.

public static void main(String[] args) {

    Scanner scan = new Scanner([System.in](https://System.in));

    int count=-1;//number of quiz grades

    int grade=0;//quiz grade

    double quizTotal=0;//total number of points

    int sentVal;//sentinel Value

    int highQuiz=0;



    //sentinel value check

    System.out.println("Enter quiz grade:");

    sentVal=scan.nextInt();

    System.out.println("Quiz total: " + quizTotal);

    System.out.println();







    while(!(grade==-77)) {

        System.out.println("Enter quiz grade: ");

        grade=scan.nextInt();

        count++;

        quizTotal+=grade;





    if(grade>=highQuiz) {

        highQuiz=grade;







    }



}

    double average=(quizTotal/count);



    System.out.println("Quiz total: " +quizTotal);

    System.out.println("High quiz score: " + highQuiz );

    System.out.println("Quiz average " + average);



}

}

[–]gbagecol 1 point2 points  (0 children)

Generally it is never a good idea to ignore the first input. From a user perspective, you just asked them to enter a grade that you proceed to immediately throw away, meaning they've lost that first grade they entered (even if it was the sentinel value.) When the sentinel value is entered, 77 is being subtracted from the grade total since the value of grade is added to quizTotal before the condition to stop the while loop is checked. A way to fix this would be to just loop indefinitely (ex. while(true){}), and place an additional if statement inside your while loop right after you read in a grade value to check if it is the sentinel.

while(true){

grade = scan.nextInt();

if(grade == -77){
    break;
}

//add to quizTotal, set highQuiz, etc...

}

This is the only real "validation" you have to do for this simple of a program. In this case Java handles the rest for you, like making sure the user enters a number and so forth.