Reputation: 49
I am new to programming. I was trying to make blackjack and I ran into some problems.
My console output:
Would you like to play again? y/n
y
You have: $50
Whats your bet:
12
You get a 7 and a 10
Your total is 17
The dealer has a 9 11 showing
Would you like to hit? y/n :
n
The dealer has 20 //Here it is supposed to say You lost blah blah blah
Would you like to play again? y/n //but its skipping to System.println("play again");
package loops;
import java.util.Scanner;
import java.util.Random;
public class loops {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
char hit = 0;
char playAgain = 0;
String dolphin = "banana";
int c = 5;
int win = 0;
int[] cards = {2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11};
int bet, money = 999999999, cardValue = 0, dealersValue = 0;
if(money == 999999999){
System.out.println("How much money would you like to start with?");
money = reader.nextInt();
}
do {
shuffleArray(cards);
System.out.println("You have: $" + money);
System.out.println("Whats your bet: ");
bet = reader.nextInt();
if(bet > money){
System.out.println("You cannot bet over " + money + "!");
System.exit(0);
break;
}
System.out.println("You get a " + cards[0] + " and a " + cards[1]);
cardValue = cards[0] + cards[1];
System.out.println("Your total is " + cardValue);
if(cardValue == 21){
win = 1;
}
if(cardValue > 21){
win = 2;
}
if(win != 2){
int i = 9;
System.out.println("The dealer has a " + cards[7] + " " + cards[8] + " showing");
dealersValue = cards[7] + cards[8];
System.out.println("Would you like to hit? y/n : ");
hit = reader.next().charAt(0);
while(dealersValue <= 17){
if(dealersValue <= 17){
dealersValue = cards[7] + cards[8] + cards[i];
}
if(dealersValue <= 17){
dealersValue = cards[7] + cards[8] + cards[i] + cards[i];
}
if(dealersValue <= 17){
dealersValue = cards[7] + cards[8] + cards[i] + cards[i] + cards[i];
}
if(cards[i] > 11 && dealersValue > 21){
cards[i] = 1;
}
if(cards[7] > 11 && dealersValue > 21){
cards[7] = 1;
}
if(cards[8] > 11 && dealersValue > 21){
cards[8] = 1;
}
i++;
}}
while(hit == 'y'){
System.out.println("You get a " + cards[c]);
cardValue = cardValue + cards[c];
System.out.println("Your total is " + cardValue);
if(cardValue > 21){
if(cards[5] > 11 && cardValue > 21){
cards[5] = 1;
System.out.println("To avoid busting, you 11 changes to 1");
}
if(cards[6] > 11 && cardValue > 21){
cards[6] = 1;
System.out.println("To avoid busting, you 11 changes to 1");
}
if(cards[7] > 11 && cardValue > 21){
cards[7] = 1;
System.out.println("To avoid busting, you 11 changes to 1");
}
if(cards[8] > 11 && cardValue > 21){
cards[8] = 1;
System.out.println("To avoid busting, you 11 changes to 1");
}
if(cards[9] > 11 && cardValue > 21){
cards[9] = 1;
System.out.println("To avoid busting, you 11 changes to 1");
}
if(cardValue > 21){
break;
}
}
System.out.println("Would you like to hit? y/n : ");
hit = reader.next().charAt(0);
c++;
if(hit =='n'){
System.out.println("You stay!");
cardValue = cardValue + 0;
break;
}
}
System.out.println("The dealer has " + dealersValue);
if(dealersValue > 21){
win = 1;
}
if(cardValue > 21){
win = 2;
}
if(cardValue == 21){
win = 1;
}
if(dealersValue == 21){
win = 2;
}
if (dealersValue > cardValue && dealersValue > 21){
win = 2;
}
if (dealersValue == cardValue){
win = 2;
}
if(dealersValue < cardValue && cardValue <=21) {
win = 1;
}
if(dealersValue == 21 && dealersValue != cardValue || dealersValue == 21 && dealersValue == cardValue || cardValue > 21){
win = 2;
}
if(cardValue == 21 && dealersValue != cardValue || dealersValue > 21){
win = 1;
}
if(win == 1){
System.out.println("You won "+bet*1 + " dollars!");;
System.out.println("Your total balance is " + money);
}
if(win == 2){
System.out.println("You lost "+bet*1 + " dollars!");;
System.out.println("Your total balance is " + money);
}
if(win == 3){
System.out.println("You lost "+bet*1 + " dollars!");;
System.out.println("Your total balance is " + money);
}
if(money <= 0){
System.out.println("You are out of money!");
System.exit(0);
}
System.out.println("Would you like to play again? y/n ");
playAgain = reader.next().charAt(0);
if(playAgain == 'n'){
System.exit(0); }
win = 0;
} while (playAgain == 'y');
}
static void shuffleArray(int[] cards){
Random rnd = new Random();
for (int i = cards.length - 1; i > 0; i--)
{
int index = rnd.nextInt(i + 1);
int a = cards[index];
cards[index] = cards[i];
cards[i] = a;
}
}
}
I've combed through the code multiple times, looking for where I went wrong, like maybe a }
where it shouldn't be, but I could not find anything. I think it could use some fresh eyes!
After I get this code working, I'm planning on trying again with an object oriented approach.
Upvotes: 1
Views: 1286
Reputation: 6844
The set of if statements that set the variable "win" are incorrect.
Walking through the code with the example given (dealer has 20, player has 17), the variable "win" is never set thus all the statements printing out "You lost" and "You won" are skipped.
It is very likely that in this line
if (dealersValue > cardValue && dealersValue > 21){
you meant
dealersValue < 21
which would then be cause win to be set to 2 and the correct output to be displayed.
Debugging Tip: Walk through the code with your IDE. If you step through line by line, you should immediately see it skip of setting the "win" variable.
Good luck in learning programming! It's fun but very frustrating :)
Upvotes: 2