Home > Enterprise >  How to create a lowscore tracker?
How to create a lowscore tracker?

Time:11-12

I have a code that displays a game where the lowest score is the aim. In my code, the game can replay as many times as I want and is supposed to display the lowest score from all of the game plays. The changing variable is currentScore. It changes once the game is complete.

The current score can be between 0 and 80.

Here is what I've come up with that is always returning a "NEW LOW SCORE" when it shouldn't always be.

int lowestScore = 0;

if (currentScore < lowestScore) {
    
    lowestScore = currentScore;
    
    System.out.println("Score = "   currentScore   "  NEW LOW SCORE!");
    System.out.println("Lowest Score = "   lowestScore);
    
}
else {
    System.out.println("Score = "   currentScore);
    System.out.println("Lowest Score = "   lowestScore);
}

The output is supposed to show the lowest score value, regardless if it is a new lowest score or not. If it is a new lowest score, it is supposed to show "NEW LOW SCORE" and if not, just the current score and the previous lowest score.

I understand that this if statement doesn't work but I am truly stuck. Any ideas?

Here is a test case of my issue.

game runs and score is 45 outprint: Score = 45 NEW LOW SCORE! Lowest Score = 45

same sequence and game runs again, score is 78 outprint: Score = 78 NEW LOW SCORE! Lowest Score = 78

This is incorrect because in this case, this is what the output should look like:

game runs and score is 45 outprint: Score = 45 NEW LOW SCORE! Lowest Score = 45

same sequence and game runs again, score is 78 outprint: Score = 78 Lowest Score = 45

CodePudding user response:

try this.

int lowestScore = 0;

if(lowestScore == 0){
lowestScore=currentScore;
}
        if (lowestScore > currentScore) {

            lowestScore = currentScore;

            System.out.println("Score = "   currentScore   "  NEW LOW SCORE!");
            System.out.println("Lowest Score = "   lowestScore);

        }
        else {
            System.out.println("Score = "   currentScore);
            System.out.println("Lowest Score = "   lowestScore);
        }

as its a loop of games, when ever the user scores the least that will be stored in lowestScore.

CodePudding user response:

You should set an initial value that reflect the genesis lowest score: the maximum possible value reflecting that there has been no previous game round:

int lowestScore = Integer.MAX_VALUE;

if (currentScore < lowestScore) {
    lowestScore = currentScore;
    System.out.println("Score = "   currentScore   "  NEW LOW SCORE!");
    System.out.println("Lowest Score = "   lowestScore);
    
} else {
    System.out.println("Score = "   currentScore);
    System.out.println("Lowest Score = "   lowestScore);
}

CodePudding user response:

You should store the lowest score in a variable you don´t override.

If you are using a database this is the place to store it. If you are doing an in memory system you should use a place where the value is not constantly overriden.

It is not recommended, but in this case, it is possible to use a static variable, so it will be settled on the start up and then won´t override it again each call.

static int lowestScore = Integer.MAX_VALUE;

I suggest you to set the first value to Integer.MAX_VALUE so it won´t need to change in case your maximum changes from 80 to 120 for example.

  • Related