Results 1 to 3 of 3
- 11-30-2011, 04:08 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 4
- Rep Power
- 0
Can I use one switch for multiple expressions?
The tittle says it all!
At the moment I am working on a (noob) project to make a pokerdice game. I've made 5 objects (dices).
I need them now to run one by one in a switch check to make random numbers from 1-6 into king, dame, ace, etc...
My code in where the problem occurs.
So by switch (dobbel1) I would like to run all dobbel true it.Java Code:public void roll() { Random rand = new Random(); dobbel1 = rand.nextInt(6) + 1; dobbel2 = rand.nextInt(6) + 1; dobbel3 = rand.nextInt(6) + 1; dobbel4 = rand.nextInt(6) + 1; dobbel5 = rand.nextInt(6) + 1; } public String toString(){ String resultaat; switch (dobbel1){ case 1: resultaat = "aas"; break; case 10: resultaat = "Boer"; break; case 11: resultaat ="Dame"; break; case 12: resultaat ="Koning"; break; default: resultaat = "" + waarde ; } resultaat += " " + kleur; return resultaat; }
Maybe with a for lus?
How can I make this work? And I think 5 switches isn't the right answer ;)
I hope you understand me in this crappy english.
Thanks in advance
Karibo
-
Re: Can I use one switch for multiple expressions?
Rather than dobbel1, dobbel2, ... why not an array: int[] dobbels = new int[5], then use a for loop to do the code above for each dobbel in the array.
- 11-30-2011, 04:17 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Regular Expressions Help
By Death Sickle in forum New To JavaReplies: 4Last Post: 04-04-2011, 04:21 AM -
regular expressions
By sozeee in forum New To JavaReplies: 3Last Post: 12-06-2010, 09:58 PM -
Solution to Create Multiple Form and Switch between them Easy
By vdparaliya in forum Java AppletsReplies: 0Last Post: 06-14-2010, 05:21 AM -
JSP Expressions
By Java Tip in forum Java TipReplies: 0Last Post: 12-26-2007, 10:13 AM -
JSP Expressions
By Java Tip in forum Java TipReplies: 0Last Post: 12-02-2007, 09:35 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks