1

I want to message the player if he score a specific score.

What I am currently using ..

if(score <= 4){
 messagePlayer("You scored 1.");

} else if(score <= 19){
 messagePlayer("You scored 1.");
 messagePlayer("You scored 5.");

} else if(score <= 49){
 messagePlayer("You scored 1.");
 messagePlayer("You scored 5.");
 messagePlayer("You scored 20.");

} else if(score <= 99){
 messagePlayer("You scored 1.");
 messagePlayer("You scored 5.");
 messagePlayer("You scored 20.");
 messagePlayer("You scored 50.");

} else if(score >= 100){
 messagePlayer("You scored 1.");
 messagePlayer("You scored 5.");
 messagePlayer("You scored 20.");
 messagePlayer("You scored 50.");
 messagePlayer("You scored 100.");
}

The scores I want are:

  • 1 Point
  • 5
  • 20
  • 50
  • 100

Is my logic 100% right and accurate? I think my first one score <= 4 is wrong, so if he gets 0 he can also get the message You scored 1. And I am not using score == x, because I need to message the player at the end, when he finish, so his highscore like 15.

2
  • What about doing some JUnit test? Commented May 16, 2014 at 15:56
  • I cannot do a lot of tests, cuz the messagePlayer method only message the player once then saves that he got messaged in a preference, then it will be ignored if he scores the same next time, that is why he can get messaged for both scores .. Commented May 16, 2014 at 15:58

5 Answers 5

3

Explicitly exclude zero like this:

if(score > 0 && score <= 4){
 messagePlayer("You scored 1.");

}

Or better yet, store your boundary values in an array and solve the problem in one loop:

int[] boundaries = { 1, 5, 20, 50, 100 };

for (int i : boundaries) {
    if (score >= i) {
        messagePlayer("You scored " + i);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

thats for 1, so I can use the same for other? like if(score > 4 && score <= 19){ for 5 .. and if(score > 19 && score <= 49){ for 20 .. etc? will work fine?
1

You can make this code much more readable:

if(score <= 4)
    messagePlayer("You scored 1.");
if(score <= 19)
    messagePlayer("You scored 5.");
if(score <= 49)
    messagePlayer("You scored 20.");
if(score <= 99)
    messagePlayer("You scored 50.");
if(score >= 100)
    messagePlayer("You scored 100.");

1 Comment

That's not more readable, infact that coding style is deemed bad coding practice by many, and allows for easy mistakes.
1

So you need to also check in your first if statement for higher than zero result.You could alter your first if statement like this

if(score>0 && score <= 4)

Comments

1

i will modify little bit your structure

if( score >0){
 messagePlayer("You scored 1.");
} 
if(score >4){
 messagePlayer("You scored 5.");

} 
if(score > 19){
 messagePlayer("You scored 20.");

}

if(score > 49){
 messagePlayer("You scored 50.");

} 
if(score > 99){
 messagePlayer("You scored 100.");
}

note that, i removed else statements

2 Comments

but if I score 75 .. wouldn't it be confused to go to if(score > 19){ or if(score > 49){ or if(score >4){ or if(score >0){ ?
@JohnJared it will satisfy all conditions and will display all messages, same like you had before
1

The program as it exists now will always tell the user they scored 1, as long as they scored at least one, because your if-else will never make it past the first if, which will evaluate to true.

Better usage really would be to loop through your target scores an message each as the user crossed that threshold:

int[] targets = {0,1,5,20,50,100}

for (int i : targets) {
 if (score >= i) {
  messagePlayer("You scored " + i + ".")
 }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.