File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Nesting My If statements

 
Sean Magee
Ranch Hand
Posts: 69
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
David Ulicny
Ranch Hand
Posts: 724
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe the switch - case will be better to use.
 
Mahesh Bhatt
Ranch Hand
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it does, thanks Guys!
 
Sean Magee
Ranch Hand
Posts: 69
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • 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:
 
Tony Morris
Ranch Hand
Posts: 1608
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • 0
  • Mark post as helpful
  • send pies
  • 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 agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic