• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Nesting My If statements

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello! I know the code below can be done in much
better ways than presented. I am trying to make
a black jack simulator of some sort. I am just starting
the beginning, however my output isnt comign out
right. I know this is due to the way I am looping
the if statements. I am new to this, and any help will be
greatly appreciated.






When I run the program, it only does Spades and Diamonds,
and never goes past the number 11, even though random is supposed
to select 12 + 13.,
and other strange things. Can someone point me in the right
direction?

[ October 08, 2004: Message edited by: Sean Magee ]
[ October 08, 2004: Message edited by: Sean Magee ]
 
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the docs what is said about Math.random()
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.

Than 12*0.9 = 10.8 for example. You will never get value higher than 11.
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe the switch - case will be better to use.
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
The best option ofcourse will be to use a switch (case) that would not only make the code shorter but also avoid all that if-else dissappear from your code.

hope it helps.
 
Sean Magee
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it does, thanks Guys!
 
Sean Magee
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so i decided to take the advice given, and instead of putting them

into if statements, I went switch.





So that was implemented. However, im having an error saying that

"set might not have been initialized" Well set is declared.

Any help?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the compiler can't tell that you're garanteed to get a number 0-3 inclusive. it's telling you that if your color_2 is, say, an 8, you will never give a value to set.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you declare the variable set, you should give it some initial value. Some friends of mine recently suggested using nonsense initial values that can never possibly occur in the program. This helps later when you need to debug because if you see the nonsense value you can track down the problem a little easier. For example you can do something like:
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the Java language got rid of the switch/case construct, it would be one step closer to purism.
Almost always, these type of problems are better implemented as a Map rather than a switch/case construct.

Being the purist that I am, I haven't used a switch/case since last century some time - others might not have such a purist perspective and so will not concur.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why not take a LazyProxyPooledAndCachedFactoryObjectFactorySomething?

just for the purists, I have a very pure (poor?) idea:


The number of sets will not change for the next 400 years, I guess.
[ October 11, 2004: Message edited by: Stefan Wagner ]
 
I didn't say it. I'm just telling you what this tiny ad said.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic