Results 1 to 3 of 3
  1. #1
    karibo is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Question 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.
    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;
        }
    So by switch (dobbel1) I would like to run all dobbel true it.
    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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    karibo is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Can I use one switch for multiple expressions?

    Wow thanks for fast response! :)

    Yes i'm going to try this never seen arrays but it seems logic.

    Thanks alot :)!

Similar Threads

  1. Regular Expressions Help
    By Death Sickle in forum New To Java
    Replies: 4
    Last Post: 04-04-2011, 05:21 AM
  2. regular expressions
    By sozeee in forum New To Java
    Replies: 3
    Last Post: 12-06-2010, 10:58 PM
  3. Replies: 0
    Last Post: 06-14-2010, 06:21 AM
  4. JSP Expressions
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-26-2007, 11:13 AM
  5. JSP Expressions
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-02-2007, 10:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •