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

• November 15th, 2013, 09:39 AM
tonynsx
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:

Code :

```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

Code :

``` 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; }```
• November 15th, 2013, 10:37 AM
Norm
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?
• November 15th, 2013, 10:53 AM
tonynsx
Re: Help with a class (only receiving the last value instead of all)
Quote:

Originally Posted by Norm
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

Code :

``` 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; }```