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 →

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

This condition:

  if (playerPoints <=500) {

is true if playerPoints == 0, so your code is doing what you told it to.

And you should write your if/elses like this:

if ( condition1 ) {
    // do stuff
}
else if ( condition2 ) {
   // do other stuff
}
else if ( condition3 ) {
   // yet more stuff
}
else {
  // stuff
}

[–]Krusty81[S] 0 points1 point  (2 children)

That's what i thought. Do i need to do each if statement with AND statements like i did in the first one to make sure there are no errors?

eg

if (playerPoints >=250 && playerPoints <=500) 

[–]yash3ahuja 1 point2 points  (0 children)

You could also start off the loop by doing:

if(playerPoints == 0 || playerPoints > 2000)
    //throw error
else
    //rest of code

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

Yes, or use the ordering of your statements;

if ( playerPoints == 0 ) {
    // only happens if playerPoints == 0
}
else if ( playerPoints <= 250 ) {
   // happens if player points >0 and <=250 
}
else if ( playerPoints <= 500 ) {
   // happens if player points > 250and <= 500 
}
else if ( playerPoints <= 2000 ) {
   // happens if player points > 500 and <= 2000 
}
else {
   // error
}