aspose file tools*
The moose likes Beginning Java and the fly likes Nesting My If statements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Nesting My If statements" Watch "Nesting My If statements" New topic
Author

Nesting My If statements

Sean Magee
Ranch Hand

Joined: Aug 23, 2004
Posts: 69
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 ]
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
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.


SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
Maybe the switch - case will be better to use.
Mahesh Bhatt
Ranch Hand

Joined: Sep 15, 2004
Posts: 88
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.


Impossible is I M Possible
Sean Magee
Ranch Hand

Joined: Aug 23, 2004
Posts: 69
Yes it does, thanks Guys!
Sean Magee
Ranch Hand

Joined: Aug 23, 2004
Posts: 69
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?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11476
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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:


Java API Documentation
The Java Tutorial
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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.


Tony Morris
Java Q&A (FAQ, Trivia)
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

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 ]

http://home.arcor.de/hirnstrom/bewerbung
 
Don't get me started about those stupid light bulbs.
 
subject: Nesting My If statements