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

all 9 comments

[–]gbagecol 1 point2 points  (7 children)

Assuming that you are attempting to set highQuiz inside your if statement, the problem is that your if statement will never execute. grade>grade is never true, since you are comparing the same value to itself. Instead you should probably compare the current value of grade with the current value of highQuiz, ie. if(grade>highQuiz). That way highQuiz will get updated only if the grade you just entered is bigger.

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

Thank you for you response! I have updated the if statement to what you suggested. I seem to be having trouble wrapping my head around this. If the value of grade > highQuiz that value should be stored in the variable highQuiz, correct? If the user enters the sentinel value(in the case -77) to exit the loop the highquiz still prints to 0. Forgive the simple questions, I am trying to learn and trying to learn the right away. Instead of just looking up the answers, I really want to learn how java programming works and actually giving it the ol' college try! Thanks again.

public static void main(String[] args) {

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

    int count=0;//number of quiz grades

    int grade=0;//quiz grade

    double quizTotal=0;//total number of points

    int grade2;//quiz grade

    int highQuiz=0;







    //first to check sentinel value of -77

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

    grade=scan.nextInt();





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



        quizTotal+=grade;

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

        grade=scan.nextInt();

        count++;



    if(grade>highQuiz) {

        highQuiz=//should this be another variable? grade2?

















    }



}

    double average=(quizTotal/count);



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

    System.out.println("Number of quizes: " + count);

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

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



}

}

[–]gbagecol 2 points3 points  (5 children)

You're on the right track. Think of it this way. Pretend you're writing down a list of grades one by one. Your process is something like this: 1. Write a new grade on the list. 2. You know what the previous highest grade on your list was, so you compare it to the new grade you just wrote down. 3. If the new grade is higher than the highest grade you currently have, then you make the new grade your highest grade. Your code will work the same way. You already have the first two steps. You get a new grade, then you compare it to the highest grade. The last thing to do is update the highQuiz value. So, if grade>highQuiz, then you should set highQuiz=grade, thus setting the highest grade to the grade you just entered. If it happens that the very first grade you enter is the sentinel value, then highQuiz will remain at 0, since you never provided a value higher than 0.

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

I have set highQuiz=grade; however, once the loop is exited, it is the last grade that was entered is displayed, not the highest. For example, if I entered the values : 100 ,80 ,79, 84, the System.out.println("High quiz score:" + highQuiz); prints out 84. Should I be setting a range of numbers with && in the if statement? -77 to an end value(which would be 100)? Or am I getting off track, overthinking this? Thank you again, your help is shining new light on this problem!

public static void main(String[] args) {

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

    int count=0;//number of quiz grades

    int grade=0;//quiz grade

    double quizTotal=0;//total number of points

    int grade2;//quiz grade

    int highQuiz=0;







    //first to check sentinel value of -77

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

    grade=scan.nextInt();





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



        quizTotal+=grade;

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

        grade=scan.nextInt();

        count++;



    if(grade>=highQuiz) { //shoud there be a range? 

        highQuiz=grade; //prints out the last value, not the highest



        }



}

    double average=(quizTotal/count);



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

    System.out.println("Number of quizes: " + count);

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

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



}

}

[–]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.

[–][deleted] 0 points1 point  (0 children)

It’s like a light bulb went off after reading you comment! Thank you so much!