Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Switch doesn't work properly

  1. #1
    Junior Member
    Join Date
    Jul 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Switch doesn't work properly

    Can someone help me with this?
    Maybe it's a stupid question but I cant figure it out. I try to make a simple card game, and after the shuffle the player should get a random card. I created this to select a random card out of the deck, but it only shows cards wich are spades. It's probably a small error but I don't see it.
    playerCardOne.onclick = () => {
        let color = Math.floor(Math.random() * 4);
        let card = Math.floor(Math.random() * 13);
        switch (color) {
            case 0:
                switch (card) {
                    case 0:
                        playerCardOne.src = club1;
                        break;
                    case 1:
                        playerCardOne.src = club2;
                        break; 
                    case 2:
                        playerCardOne.src = club3;
                        break;
                    case 3:
                        playerCardOne.src = club4;
                        break;
                    case 4:
                        playerCardOne.src = club5;
                        break;
                    case 5:
                        playerCardOne.src = club6;
                        break;
                    case 6:
                        playerCardOne.src = club7;
                        break;
                    case 7:
                        playerCardOne.src = club8;
                        break;
                    case 8:
                        playerCardOne.src = club9;
                        break;
                    case 9:
                        playerCardOne.src = club10;
                        break;
                    case 10:
                        playerCardOne.src = club11;
                        break;
                    case 11: 
                        playerCardOne.src = club12;
                        break;
                    case 12:
                        playerCardOne.src = club13;
                        break;
                    default:
                        break;
                }
            case 1:
                switch (card) {
                    case 0:
                        playerCardOne.src = heart1;
                        break;
                    case 1:
                        playerCardOne.src = heart2;
                        break; 
                    case 2:
                        playerCardOne.src = heart3;
                        break;
                    case 3:
                        playerCardOne.src = heart4;
                        break;
                    case 4:
                        playerCardOne.src = heart5;
                        break;
                    case 5:
                        playerCardOne.src = heart6;
                        break;
                    case 6:
                        playerCardOne.src = heart7;
                        break;
                    case 7:
                        playerCardOne.src = heart8;
                        break;
                    case 8:
                        playerCardOne.src = heart9;
                        break;
                    case 9:
                        playerCardOne.src = heart10;
                        break;
                    case 10:
                        playerCardOne.src = heart11;
                        break;
                    case 11: 
                        playerCardOne.src = heart12;
                        break;
                    case 12:
                        playerCardOne.src = heart13;
                        break;
                    default:
                        break;
                }
            case 2:
                switch (card) {
                    case 0:
                        playerCardOne.src = diamond1;
                        break;
                    case 1:
                        playerCardOne.src = diamond2;
                        break; 
                    case 2:
                        playerCardOne.src = diamond3;
                        break;
                    case 3:
                        playerCardOne.src = diamond4;
                        break;
                    case 4:
                        playerCardOne.src = diamond5;
                        break;
                    case 5:
                        playerCardOne.src = diamond6;
                        break;
                    case 6:
                        playerCardOne.src = diamond7;
                        break;
                    case 7:
                        playerCardOne.src = diamond8;
                        break;
                    case 8:
                        playerCardOne.src = diamond9;
                        break;
                    case 9:
                        playerCardOne.src = diamond10;
                        break;
                    case 10:
                        playerCardOne.src = diamond11;
                        break;
                    case 11: 
                        playerCardOne.src = diamond12;
                        break;
                    case 12:
                        playerCardOne.src = diamond13;
                        break;
                    default:
                        break;
                }
            case 3:
                switch (card) {
                    case 0:
                        playerCardOne.src = spade1;
                        break;
                    case 1:
                        playerCardOne.src = spade2;
                        break; 
                    case 2:
                        playerCardOne.src = spade3;
                        break;
                    case 3:
                        playerCardOne.src = spade4;
                        break;
                    case 4:
                        playerCardOne.src = spade5;
                        break;
                    case 5:
                        playerCardOne.src = spade6;
                        break;
                    case 6:
                        playerCardOne.src = spade7;
                        break;
                    case 7:
                        playerCardOne.src = spade8;
                        break;
                    case 8:
                        playerCardOne.src = spade9;
                        break;
                    case 9:
                        playerCardOne.src = spade10;
                        break;
                    case 10:
                        playerCardOne.src = spade11;
                        break;
                    case 11: 
                        playerCardOne.src = spade12;
                        break;
                    case 12:
                        playerCardOne.src = spade13;
                        break;
                    default:
                        break;
                }
            default:
                break;
        }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Switch doesn't work properly

    Can you post the output that shows what you are talking about
    Can you post the declarations for all 52 values used in the code so it can be compiled for testing?

    What are the values of card and color that are set by random? Add code to print them for debugging.

    What language is the code using? I do not recognize the let statement.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jul 2019
    Posts
    36
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Re: Switch doesn't work properly

    your code is Javascript, and this forum is for Java language

  4. The Following User Says Thank You to zemiak For This Useful Post:

    Norm (July 13th, 2019)

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Switch doesn't work properly

    Moved out of java programming section.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Jul 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Switch doesn't work properly

    Ah, I'm really sorry. Didn't realize that.

Similar Threads

  1. Switch Statement with multpile char variables not working properly
    By ravinniaofcreed in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 22nd, 2017, 08:29 PM
  2. Need help on making a mastermind game program work properly
    By aude922000 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2013, 07:42 PM
  3. Can't get the loop to work properly
    By thegreatzo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 9th, 2012, 02:57 PM
  4. [SOLVED] cannot get this method to work properly
    By Nismoz3255 in forum Loops & Control Statements
    Replies: 2
    Last Post: May 2nd, 2011, 12:00 PM
  5. I cannot get boolean to work properly
    By kl2eativ in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 18th, 2011, 07:37 AM