Blackjack Counting (JAVA HELP PLEASE)

Back to General discussions forum

Java_Coder     2015-06-19 01:21:01

I have been working on this code for hours, and I can't seem to figure out why it's not working for me. Could someone point out my mistakes it would be greatly appreciated.I think I have the right idea, but I'm not sure. Here is what I have so far

import java.util.*;
import java.io.*;

public class GameOBlackJack 
{

    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);

        System.out.print("Input data: ");
        int inputN = scan.nextInt();
        int total = 0;
        int ace_count = 0;
        char[] c = {'2','3','4','5','6','7','8','9','T','J','Q','K','A'};

        for(int i = 0; i <= inputN; i++)
        {
            char cards = scan.next().charAt(0);
            for(int card = 0; card <=  cards; card++)
            {
                if(card == 'A')
                    ace_count += 1;
            }

                while(total > 21 && ace_count > 0)
                {
                    total -= 10;
                    ace_count -= 1;
                }
                if(total > 21)
                    System.out.print("Bust");
                else
                    System.out.print(total);
            }
    }
}

Any help is greatly appreciated.

Thanks All, Java_Coder

Rodion (admin)     2015-06-19 07:08:26
User avatar

Hi! Thanks for your message!

I had a look at your code and I'm afraid I could not find where you increase total variable at all. Probably you've forgotten to add some piece of code related to this. Hope this idea may help :)

Java_Coder     2015-06-19 23:24:14

Okay thankyou for the responce!

Java_Coder     2015-06-19 23:29:54

I've revised my code, and found where I needed to increment the total variable. It still for some reason won't work :(. Here is my new code:

import java.util.*;
import java.io.*;

public class GameOBlackJack 
{

    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);

        System.out.print("Input data: ");
        int inputN = scan.nextInt();
        int total = 0;
        int ace_count = 0;
        char[] c = {'2','3','4','5','6','7','8','9','T','J','Q','K','A'};

        for(int i = 0; i <= inputN; i++)
        {
            char cards = scan.next().charAt(0);

            for(int card = 0; card <=  cards; card++)
            {
                total+=c[card];    //I added this new line of code
                if(card == 'A')
                    ace_count += 1;
            }

                while(total > 21 && ace_count > 0)
                {
                    total -= 10;
                    ace_count -= 1;
                }
                if(total > 21)
                    System.out.print("Bust");
                else
                    System.out.print(total);
            }
    }
}
Rodion (admin)     2015-06-20 05:18:46
User avatar

Hi!

Check, how many times scan.next() is called, and how many times you really whant it to be called.

See, I think I can tell at once what is incorrect - and moreover I can write proper code myself. But probably you want to learn how to write and fix code yourself, right? :) So allow me at first to try and propose a couple of hints which may help you in this.

Have you tried to debug your code? Usually your IDE allows step-by-step execution (you can google for instructions) and watching current value of variables.

Even if you do not use IDE, you can for example add debugging output - printing intermediate values of some variables. Like this:

System.out.println("DEBUG: iteration #" + i); // I added this...
for(int card = 0; card <=  cards; card++)
{
    total+=c[card];    //I added this new line of code
    if(card == 'A')
        ace_count += 1;
    System.out.println("DEBUG: total is " + total); // ...and this
}

Another thing: when your program "do not work", you should try to explain precisely what do you mean - i.e. does it compile, run, does it provide necessary amount of answers and whether all answers are wrong or only some of them. Usually this can help not only other people to understand you better, but probably you yourself can understand what may be wrong.

Good luck and feel free to ask if you still have troubles!

Please login and solve 5 problems to be able to post at forum