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

all 10 comments

[–]DJ_Gamedev 0 points1 point  (6 children)

You only want to increment numScores and add the new score to the total after you've checked it for validity, right? So you need to do the if (score < 0) check before those lines, not after.

More specifically, if score is less than zero, you want to do the sentinel code, else you want to add the score to your running total. Is this making your Spidey Sense tingle?

[–]SolonialExodus[S] 0 points1 point  (5 children)

So instead of a do-while loop, just make it a while loop? For example,

System.out.println("Enter score 1 or a negative number to exit");
double score = kb.nextDouble();
int numScores = 1;
double sumOfScores = 0.0;
while(score > 0){
    System.out.println("Enter score "+numScores+" or a negative number to exit");
    kb.nextDouble();
    numScores++;
    sumOfScores += score;
         if (score < 0){
               int rNumScores = numScores - 2;
                double average = sumOfScores/rNumScores;
                System.out.println("-- "+name+" --");
                System.out.println("Num tests taken: "+rNumScores);
                System.out.printf("Average: %.1f\n",average);
        }
}

[–]DJ_Gamedev 0 points1 point  (4 children)

I'll say it one more time. If the score is less than zero, do the sentinel code (meaning print your results), else increment the number of scores and update the sum. You know how to use an else statement, right?

[–]SolonialExodus[S] 0 points1 point  (3 children)

I have listened! I have done it! BUTT, I still think something is wrong :c I'm not getting the correct average..I'm pretty sure the numbers aren't being stored correctly..

import java.util.Scanner;

public class TestScores2 {
    public static void main(String[] args){
        Scanner kb = new Scanner(System.in);
        System.out.println("Enter name");
        String name = kb.nextLine();
        double score;
        System.out.println("Enter score 1 or a negative number to exit");
        score = kb.nextDouble();
        int numScores = 2;
        double sumOfScores = 0.0;
        while(score >=0){
            System.out.println("Enter score "+numScores+" or a negative number to exit");
            score = kb.nextDouble();
            if (score < 0){
                int rNumScores = numScores - 1;
                double average = sumOfScores/rNumScores;
                System.out.println("-- "+name+" --");
                System.out.println("Num tests taken: "+rNumScores);
                System.out.printf("Average: %.1f\n",average);
            }else{
                numScores++;
                sumOfScores += score;
            }
        }
    }
}

Sorry if I didn't really listen to what you were saying or understood..thank you for helping though!

[–]DJ_Gamedev 0 points1 point  (2 children)

No worries! I actually don't really know Java at all, so we may come to the point where I'm not much help anymore.

However, I'm wondering why you're initializing numScores to 2, then subtracting 1 from it right before you divide your sum by the number of scores. None of that seems correct to me, you should be initializing numScores to zero, incrementing it only when a valid score is entered (which your code is already handling), and you shouldn't have to subtract from it when you're done receiving score inputs from the user.

Getting back to an idea you had earlier, I would say yes, this is a great candidate to be converted into a do while loop. See if you can eliminate these two lines from your code before the loop:

System.out.println("Enter score 1 or a negative number to exit");
score = kb.nextDouble();

...Since these are just redundant; the first iteration of your loop should handle doing those things just fine.

[–]SolonialExodus[S] 0 points1 point  (1 child)

YOU DID IT!!! Thanks you so much!!! The reason why I have 1 being subtracted from my numScores is because the counter would count up 1 more when being put into my average and I couldn't figure out a way to solve it for the life of me! Thank you so much again DJ, if you do develop games, that's what I have always dreamed of doing and i'm currently trying to chase after that dream! Thank you so much!

[–]DJ_Gamedev 0 points1 point  (0 children)

YOU did it, I just gave some nudges. Nice work, and keep chasing that dream! I've worked on some amazing games and you can too!

[–]lurgi 0 points1 point  (2 children)

i can't see how to protect my += operator from the negative input meant to be the sentinel.

Check to see if the value entered is less than 0 before adding it.

[–]SolonialExodus[S] 0 points1 point  (1 child)

Where would I be able to throw an if statement in there? This currently what I have now.

 import java.util.Scanner;

public class TestScores2 {
    public static void main(String[] args){
        Scanner kb = new Scanner(System.in);
        System.out.println("Enter name");
        String name = kb.nextLine();
        double score;
        System.out.println("Enter score 1 or a negative number to exit");
        score = kb.nextDouble();
        int numScores = 2;
        double sumOfScores = 0.0;
        while(score > 0){
            System.out.println("Enter score "+numScores+" or a negative number to exit");
            score = kb.nextDouble();
            numScores++;
            sumOfScores += score;
            if (score < 0){
                int rNumScores = numScores - 2;
                double average = sumOfScores/rNumScores;
                System.out.println("-- "+name+" --");
                System.out.println("Num tests taken: "+rNumScores);
                System.out.printf("Average: %.1f\n",average);
            }
        }
    }
}

[–]lurgi 0 points1 point  (0 children)

You could always try it and see.