• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

I need help with Code Cleanup

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Saloon Keeper
Posts: 9729
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First off, use else-if to eliminate nesting:

 
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something like this might help:

Edit: 44 seconds late!
 
Carey Brown
Saloon Keeper
Posts: 9729
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Secondly, replace it with a loop

 
joseph Anderson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9729
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Carey Brown
Saloon Keeper
Posts: 9729
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9729
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9729
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9729
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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'.
 
Onion rings are vegetable donuts. Taste this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic