• Post Reply Bookmark Topic Watch Topic
  • New Topic

I need help with Code Cleanup  RSS feed

 
joseph Anderson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay a few months ago I was working on a 5 card poker game and ran into a game breaking bug. Finally decided to come back to it and fix it and clean it up. While cleaning it up a came across this set of if greater than statements which I feel should be cleaned up but I am not sure how to do so. Is there a way to use a switch statement to check if scored[i] does not equal 0?(scored[i] can never be less than 0)

scored[i] and highCard are both integers and have been declared and set.


 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off, use else-if to eliminate nesting:

 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like this might help:

Edit: 44 seconds late!
 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Secondly, replace it with a loop

 
joseph Anderson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is actually a good idea but i think i figured it out



Is the logic correct here?

edit: as in does it mean the same thing as the above code
edit2: got a lot of responses while i was looking a ways of condensing it.
 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
joseph Anderson wrote:That is actually a good idea but i think i figured it out



Is the logic correct here?

edit: as in does it mean the same thing as the above code
edit2: got a lot of responses while i was looking a ways of condensing it.

This won't compile. You can't multiply a boolean by an int.
 
joseph Anderson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay realized my altered code wouldn't work let me try revising it.



how about this?
 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
joseph Anderson wrote:That is actually a good idea but i think i figured it out



Is the logic correct here?

edit: as in does it mean the same thing as the above code
edit2: got a lot of responses while i was looking a ways of condensing it.

The logic is not the same as your original post. In your original post you test the values in descending order, not ascending as you have here. You also need a break statement to avoid additional tests once a value greater than zero is found.

 
joseph Anderson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay I didn't realize you couldn't multiply booleans and ints but you can cast using (int) right? So how about this?



edit: trying to avoid break if possible. I dont want to accidently break out of the method it is in.
 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Avoiding break....

Edit: Break will only break out of the for() loop here, not the method.
 
joseph Anderson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Avoiding break....

Edit: Break will only break out of the for() loop here, not the method.


Thank you it looks nice and i really appreciate the help.
 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FYI: You can't 'break' out of a method. The only way to leave a method is to have a 'return'.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!