# help with if statements

• October 30th, 2013, 01:00 PM
tonynsx
help with if statements
In the following code, each cards and suits have specific values except for the "Ace" card. I have the correct results when the computer randomly chooses anything except for the "Ace" card. When it does select the "Ace" card, I'm getting the wrong results.

This is correct, 3 + 9 + 7 = 19

Code :

run:
Three of Spades, Nine of Hearts, Seven of Hearts,
Player total: 19
BUILD SUCCESSFUL (total time: 0 seconds)

if playerTotal + 11 is < 22, I want to add 11 to playerTotal

if playerTotal + 11 is > 22, I want to add 1 to playerTotal, both of these doesn't seem to work, sometimes I get random values for the "Ace"

This is wrong because, 2 + 7 + 11 is not 16

Code :

run:
Two of Spades, Seven of Hearts, Ace of Diamonds,
Player total: 16
BUILD SUCCESSFUL (total time: 0 seconds)

Code :

package test;

import java.util.Random;
import java.util.Scanner;

public class Test {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);
Random rand = new Random();

String[] cards = {"Ace", "Two", "Three", "four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"};
String[] suits = {" of Hearts", " of Diamonds", " of Spades", " of Clubs"};

int shuffleCards = 0, shuffleSuits, playerPoints, playerTotal;

playerTotal = 0;
playerPoints = 0;

for(int a=1; a<=3; a++)
{
shuffleCards = rand.nextInt(cards.length-1) + 0;
shuffleSuits = rand.nextInt(suits.length-1) + 0;

if(shuffleCards == 1)
{
playerPoints = 2;
}

if(shuffleCards == 2)
{
playerPoints = 3;
}

if(shuffleCards == 3)
{
playerPoints = 4;
}

if(shuffleCards == 4)
{
playerPoints = 5;
}

if(shuffleCards == 5)
{
playerPoints = 6;
}

if(shuffleCards == 6)
{
playerPoints = 7;
}

if(shuffleCards == 7)
{
playerPoints = 8;
}

if(shuffleCards == 8)
{
playerPoints = 9;
}

if(shuffleCards == 9 || shuffleCards == 10 || shuffleCards == 11 || shuffleCards == 12)
{
playerPoints = 10;
}

if((shuffleCards == 10 && shuffleSuits == 2 )|| (shuffleCards == 10 && shuffleSuits == 0) || (shuffleCards == 12 && shuffleSuits == 1))
{
playerPoints = 13;
}

if(shuffleCards == 2 && shuffleSuits == 3)
{
playerPoints = 0;
}

System.out.print(cards[shuffleCards] + suits[shuffleSuits] +", ");

playerTotal = playerTotal + playerPoints;

}

if(shuffleCards == 0 && (playerTotal + 11 < 22))
{
playerTotal = playerTotal + 11;
}

if(shuffleCards == 0 && (playerTotal + 1 > 22))
{
playerTotal = playerTotal + 1;
}

System.out.println();
System.out.println("Player total: " + playerTotal);

}
}

• October 30th, 2013, 01:09 PM
Norm
Re: help with if statements
Quote:

2 + 7 + 11 is not 16
Have you tried debugging the code by adding println() statements that print out the values of variables and expressions as the code executes? The output will show you what the code is doing.

How does the code get each of the values it adds up to get 16? What are each of the values that are added together to get 16?
• October 30th, 2013, 02:37 PM
tonynsx
Re: help with if statements
I did a system.out to show the values being counted

This works fine when there is no "ace" card selected

Code :

Three of Hearts, 3

Player total: 17

When the "ace" card is selected, it looks like it's adding some random numbers

Example 1:
Code :

Two of Hearts, 2
Six of Hearts, 6
Ace of Diamonds, 6

Player total: 15

Example 2:, here it didn't add anything to "ace" it's suppose to add 11
Code :

Ace of Hearts, 0
Nine of Diamonds, 9

• October 30th, 2013, 02:48 PM
Norm
Re: help with if statements
Add a println that shows the total and the current value to be added to the total inside the loop so it prints out each time the loop goes around.

Here's a hint to make the testing faster:
Code :

int[] cardsA = {1, 6, 0};     //<<<<get cards from here
int idx = 0;                  //  index to above array

for(int a=1; a<=3; a++)
{
shuffleCards = cardsA[idx++]; // get from array vs rand.nextInt(cards.length-1) + 0;

This will get the cards from the array so that you can be sure to get what you want for testing.

Quote:

Where is the code that is "suppose to add 11"?

Quote:

looks like it's adding some random numbers
Is 0 a random number? There are two samples you posted that both show the value of the Ace was 0.
• October 30th, 2013, 02:53 PM
tonynsx
Re: help with if statements
I think I've got it. The problem was that I didn't put the if condition for the "ace" cards in the "player points", now it seems to be working fine. I've changed my code to test it, but it looks like it's working. I changed the condition to check if the playerTotal was over 5 or less than 5

if card is ace, and playerTotal < 5, I gave, playerPoints = 11

if card is ace, and playerTotal > 5, I gave, playerPoints = 1

Working so far, now "ace" can be either 1 or 11

Code :

Seven of Diamonds, 7
Eight of Hearts, 8
Ace of Hearts, 1

Player total: 16

Code :

package test;

import java.util.Random;
import java.util.Scanner;

public class Test {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);
Random rand = new Random();

String[] cards = {"Ace", "Two", "Three", "four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"};
String[] suits = {" of Hearts", " of Diamonds", " of Spades", " of Clubs"};

int shuffleCards = 0, shuffleSuits, playerPoints, playerTotal;

playerTotal = 0;
playerPoints = 0;

for(int a=1; a<=3; a++)
{
shuffleCards = rand.nextInt(cards.length-1) + 0;
shuffleSuits = rand.nextInt(suits.length-1) + 0;

if(shuffleCards == 1)
{
playerPoints = 2;
}

if(shuffleCards == 2)
{
playerPoints = 3;
}

if(shuffleCards == 3)
{
playerPoints = 4;
}

if(shuffleCards == 4)
{
playerPoints = 5;
}

if(shuffleCards == 5)
{
playerPoints = 6;
}

if(shuffleCards == 6)
{
playerPoints = 7;
}

if(shuffleCards == 7)
{
playerPoints = 8;
}

if(shuffleCards == 8)
{
playerPoints = 9;
}

if(shuffleCards == 9 || shuffleCards == 10 || shuffleCards == 11 || shuffleCards == 12)
{
playerPoints = 10;
}

if((shuffleCards == 10 && shuffleSuits == 2 )|| (shuffleCards == 10 && shuffleSuits == 0) || (shuffleCards == 12 && shuffleSuits == 1))
{
playerPoints = 13;
}

if(shuffleCards == 2 && shuffleSuits == 3)
{
playerPoints = 0;
}

if(shuffleCards == 0 && (playerTotal < 5))
{
playerPoints = 11;
}

if(shuffleCards == 0 && (playerTotal > 5))
{
playerPoints = 1;
}

System.out.print(cards[shuffleCards] + suits[shuffleSuits] +", ");

System.out.println(playerPoints);

playerTotal = playerTotal + playerPoints;

}

System.out.println();
System.out.println("Player total: " + playerTotal);

}
}

• October 30th, 2013, 03:02 PM
Norm
Re: help with if statements
Quote:

I didn't put the if condition for the "ace" cards
That would account for it.

A comment on the chain of if statements that all convert one number to another by adding one. Why not have the random number be generated in the range that you need: 1-10 vs 0-?
You would need to change where that number is used as an array index by subtracting one in just one place instead of adding 1 in 10 places.
• October 30th, 2013, 03:28 PM
tonynsx
Re: help with if statements

I'll work on the random number generator like you said. I'll make it 0-something instead of 1-10.

It should be 0-9 right? Just from the top of my head!
• October 30th, 2013, 03:53 PM
Norm
Re: help with if statements
The code needs numbers 1-10 for card values. To get the array index, subtract 1.
• October 30th, 2013, 04:28 PM
tonynsx
Re: help with if statements
I have the program working the way I want, but I still have a question. On my if conditions for the "ace" card, I have it checking if the playerTotal is < or > 5. But, playerTotal is assigned values after the if conditions, so how is this working then? Would it work the same way if I assign playerTotal before the if conditions for the "ace" card? I'm not home now, so I can't check it, but still wondering.

Code :

if(shuffleCards == 0 && (playerTotal < 5))
{
playerPoints = 11;
}

if(shuffleCards == 0 && (playerTotal > 5))
{
playerPoints = 1;
}

System.out.print(cards[shuffleCards] + suits[shuffleSuits] +", ");

System.out.println(playerPoints);

playerTotal = playerTotal + playerPoints;