# Thread: Blackjack Program Not Calculating Totals Correctly

1. Member
Join Date
Sep 2011
Posts
56
Rep Power
0

## Blackjack Program Not Calculating Totals Correctly

I spent a while on this blackjack program. Here's how it doesn't work, This is the console:

Hello and welcome to blackjack
My name is david and i'll be your dealer
------------------------------------------------------------
Type Hit me for a card and Hold to stop the dealing
------------------------------------------------------------
Hit me
Your random card is a 0
Hit me
Your random card is a 10
Hit me
Your random card is a 4
Hold
You win! score of 4 to 4

Here's my code:

Java Code:
```import java.util.Random;
import java.util.Scanner;

public class BlackJack {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int total = 0;
int dTotal = 0;
boolean bust = false;
Init();
bust = dealerBust(dTotal, bust);
for(int i=1; i<(25); i++)
{
String input = scanner.nextLine();
if(input.equalsIgnoreCase("Hit me")){

int random = Random();
System.out.println("Your random card is a " + random);

total = random + total;
}
//Check if you went over
if (total > 21){
System.out.println("Sorry, you went over 21");
break;
}
if(input.equalsIgnoreCase("Hold")){
System.out.println("Your total stays at " + total);
Hold(bust, i, i);
break;
}
}

}

private static boolean dealerBust(int dTotal, boolean bust) {
// TODO Auto-generated method stub
for(int i=1; i<(25); i++)
{
int random1 = Random();
dTotal = random1 + dTotal;
if (dTotal >= 17){
bust = false;
break;
}
else if(dTotal > 21)
{
bust = true;
}
}
return bust;

}

private static void Hold(boolean bust,int total,int dTotal) {
if(bust == false){
if(total >= dTotal){
System.out.println("You win! score of " + total + " to " + dTotal);
}
else
{
System.out.println("You loose! score of " + total + " to " + dTotal);
}
}

}

public static int Random() {
// TODO Auto-generated method stub
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(12);
return randomInt;
}

private static void Init() {
System.out.println("Hello and welcome to blackjack");
System.out.println("My name is david and i'll be your dealer");
System.out.println("------------------------------------------------------------");
System.out.println("Type Hit me for a card and Hold to stop the dealing ");
System.out.println("------------------------------------------------------------");

}
}```

2. Member
Join Date
Oct 2011
Posts
83
Rep Power
0

## Re: Blackjack Program Not Calculating Totals Correctly

In line 31:
Java Code:
`Hold(bust, i, i);`
Why are you sending i? i is just a counter for the number of turns, when it looks like Hold() is supposed to take scores in the last two arguments.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•