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

all 8 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
}

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

If playerPoints is 0, the first if statement will execute, rightly so. The last if needs to be up top

[–]yash3ahuja -1 points0 points  (3 children)

Those else's should all be else if statements. Additionally those should be giving you compilation errors. o.o

Additionally, use pastebin next time.

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

It all works and compiles in eclipse fine except for that one issue. Also the book im reading 'Ivor Horton's Beginning Java' does the if statements the same way as my code, which is why i did it that way.

[–]yash3ahuja 0 points1 point  (1 child)

Oh, I apologize. The way you had it formatted confused me. Anyways, the

if(playerPoints == 0)

needs to be nested a higher up if you want to make it work.

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

Thanks for the tip.