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
posted
0
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
posted
0
Maybe the switch - case will be better to use.
Mahesh Bhatt
Ranch Hand
Joined: Sep 15, 2004
Posts: 88
posted
0
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
posted
0
Yes it does, thanks Guys!
Sean Magee
Ranch Hand
Joined: Aug 23, 2004
Posts: 69
posted
0
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.
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.
Never ascribe to malice that which can be adequately explained by stupidity.
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
posted
0
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:
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.