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

Thread: help with if statements

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

    Default 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

    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

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

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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,130
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: help with if statements

    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?
    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 if statements

    I did a system.out to show the values being counted

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

    Three of Hearts, 3
    Eight of Spades, 8
    Six of Spades, 6
     
    Player total: 17

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

    Example 1:
    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
    Ace of Hearts, 0
    Nine of Diamonds, 9
    Three of Spades, 3

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,130
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

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

    it's suppose to add 11
    Where is the code that is "suppose to add 11"?

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    tonynsx (October 30th, 2013)

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

    Default 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

    Seven of Diamonds, 7
    Eight of Hearts, 8
    Ace of Hearts, 1
     
    Player total: 16

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

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,130
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: help with if statements

    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.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: help with if statements

    Thanks for your help.....

    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!

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,130
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: help with if statements

    The code needs numbers 1-10 for card values. To get the array index, subtract 1.
    If you don't understand my answer, don't ignore it, ask a question.

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

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

                    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;

Similar Threads

  1. Help my if statements!
    By l2code in forum Loops & Control Statements
    Replies: 1
    Last Post: March 22nd, 2013, 01:47 PM
  2. [SOLVED] If and else if statements???
    By skeptile2 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 5th, 2013, 12:44 AM
  3. If Statements help
    By cowsquad in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 10th, 2013, 05:41 AM
  4. if else statements
    By mozyman in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 24th, 2010, 08:06 PM
  5. If statements
    By Scottj996 in forum Java Theory & Questions
    Replies: 1
    Last Post: August 16th, 2010, 10:09 AM