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 3 of 3

Thread: Help with a class (only receiving the last value instead of all)

  1. #1
    Member
    Join Date
    Jul 2013
    Location
    Baltimore, MD
    Posts
    59
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Help with a class (only receiving the last value instead of all)

    The code below, getPlayerCards(int times) shuffles the cards from the array (3) times and return the (3) values of shuffleCards, showPlayerCardValue() is suppose to take those (3) numbers and check with the if conditions to come up with a total, my problem is that, it's only checking the 3rd value, not first and second.

    ex:

    Welcome to Thirty3
     
    Would you like to play (Y/Q)? y
     
    Your cards: 6 of Hearts, 4 of Hearts, 7 of Spades, (Total 7)

    It's not adding the values of 6 of Hearts and 4 of Hears, the total should be 6+4+7 = 17

     public int getPlayerCards(int times)
        {
                System.out.print("Your cards: ");
                for(int b=1; b<=times; b++)
                {
                    shuffleCards = rand.nextInt(cards.length-1) + 0;
     
                    System.out.print(cards[shuffleCards] + ", ");                       
     
                    showCard = showCard.concat(cards[shuffleCards].concat(", "));                
                }
     
            return shuffleCards;
        }
     
        public int showPlayerCardValue()
        {   
                    if(shuffleCards >=4 && shuffleCards <= 7)
                    {
                        playerPoints = 2;
                    }
     
                    if(shuffleCards >=8 && shuffleCards <= 11)
                    {
                        playerPoints = 3;
                    }
     
                    if(shuffleCards >=12 && shuffleCards <= 15)
                    {
                        playerPoints = 4;
                    }
     
                    if(shuffleCards >=16 && shuffleCards <= 19)
                    {
                        playerPoints = 5;
                    }
     
                    if(shuffleCards >=20 && shuffleCards <= 23)
                    {
                        playerPoints = 6;
                    }
     
                    if(shuffleCards >=24 && shuffleCards <= 27)
                    {
                        playerPoints = 7;
                    }
     
                    if(shuffleCards >=28 && shuffleCards <= 31)
                    {
                        playerPoints = 8;
                    }
     
                    if(shuffleCards >=32 && shuffleCards <= 35)
                    {
                        playerPoints = 9;
                    }
     
                    if(shuffleCards >=36 && shuffleCards <= 51)
                    {
                        playerPoints = 10;
                    }
     
                    if((shuffleCards == 40)|| (shuffleCards == 42) || (shuffleCards == 49))
                    {
                        playerPoints = 13;
                    }
     
                    if(shuffleCards == 11)
                    {
                        playerPoints = 0;
                    }
     
                    if((shuffleCards >= 0 && shuffleCards<= 3) && (playerTotal <= 22))
                    {
                        playerPoints = 11;
                    }
     
                    if((shuffleCards >= 0 && shuffleCards<= 3) && (playerTotal > 22))
                    {
                        playerPoints = 1;
                    }   
     
                    playerTotal = playerPoints + playerTotal;
     
                    System.out.print("(Total " + playerTotal + ")");
     
            return playerTotal;
        }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with a class (only receiving the last value instead of all)

    Have you tried debugging the code by adding some println statements to print out the values of variables as the code executes so you can see where the code is going wrong?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jul 2013
    Location
    Baltimore, MD
    Posts
    59
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Help with a class (only receiving the last value instead of all)

    Quote Originally Posted by Norm View Post
    Have you tried debugging the code by adding some println statements to print out the values of variables as the code executes so you can see where the code is going wrong?
    Yes, It's only printing the values of the last card

    --- Update ---

    I just put inside the for loop, the count works now

        public int getPlayerCards(int times)
        {
                System.out.print("Your cards: ");
                for(int a=1; a<=times; a++)
                {
                    shuffleCards = rand.nextInt(cards.length-1) + 0;
     
                    System.out.print(cards[shuffleCards] + ", ");                       
     
                    showCard = showCard.concat(cards[shuffleCards].concat(", "));     
     
                    if(shuffleCards >=4 && shuffleCards <= 7)
                    {
                        playerPoints = 2;
                    }
     
                    if(shuffleCards >=8 && shuffleCards <= 11)
                    {
                        playerPoints = 3;
                    }
     
                    if(shuffleCards >=12 && shuffleCards <= 15)
                    {
                        playerPoints = 4;
                    }
     
                    if(shuffleCards >=16 && shuffleCards <= 19)
                    {
                        playerPoints = 5;
                    }
     
                    if(shuffleCards >=20 && shuffleCards <= 23)
                    {
                        playerPoints = 6;
                    }
     
                    if(shuffleCards >=24 && shuffleCards <= 27)
                    {
                        playerPoints = 7;
                    }
     
                    if(shuffleCards >=28 && shuffleCards <= 31)
                    {
                        playerPoints = 8;
                    }
     
                    if(shuffleCards >=32 && shuffleCards <= 35)
                    {
                        playerPoints = 9;
                    }
     
                    if(shuffleCards >=36 && shuffleCards <= 51)
                    {
                        playerPoints = 10;
                    }
     
                    if((shuffleCards == 40)|| (shuffleCards == 42) || (shuffleCards == 49))
                    {
                        playerPoints = 13;
                    }
     
                    if(shuffleCards == 11)
                    {
                        playerPoints = 0;
                    }
     
                    if((shuffleCards >= 0 && shuffleCards<= 3) && (playerTotal <= 22))
                    {
                        playerPoints = 11;
                    }
     
                    if((shuffleCards >= 0 && shuffleCards<= 3) && (playerTotal > 22))
                    {
                        playerPoints = 1;
                    }   
     
                    playerTotal = playerPoints + playerTotal;
                }
     
            return shuffleCards;
        }
     
        public int showPlayerCardValue()
        {   
            System.out.print("(Total " + playerTotal + ")");
     
            return playerTotal;
        }

Similar Threads

  1. Sending and Receiving File
    By beer-in-box in forum Java Networking
    Replies: 8
    Last Post: March 31st, 2013, 07:57 PM
  2. [Asking] Sending and Receiving Data via Serial
    By ardisamudra in forum Java Networking
    Replies: 5
    Last Post: January 23rd, 2013, 06:24 AM
  3. receiving emails through spring
    By the light in forum Web Frameworks
    Replies: 0
    Last Post: October 18th, 2011, 01:55 AM
  4. servlet not receiving any data
    By vishal21 in forum Java Servlet
    Replies: 0
    Last Post: March 14th, 2011, 01:01 PM