Tyler Bull Tyler Bull - 2 months ago 10
Java Question

global variable not recognizing user inputed numbers

import java.util.Scanner;




public class Blackjack {

class Commands {
static final String yes = "yes";
static final String no = "no";
}

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

//Opponent player2 = new Opponent();
double AiCard1 = 0;
double AiCard2 = 0;
double AiCard3 = 0;
double AiTotalcard3 = AiCard1 + AiCard2 + AiCard3;
double card1 = 0;
double card2 = 0;
double card3 = 0;
double card4 = 0;
double total2 = card1 + card2 + card3 + card4;
double total = card1 + card2 + card3;



System.out.println("Hello and Welcome to my custom version of blackjack!");
System.out.println("You will start off with $300");
System.out.println();
System.out.println("Do you want to read the rules before playing?");
System.out.print("Press 1 if yes, press 2 for no");


int choice = input.nextInt();
switch (choice) {
case 1:
System.out.println("You have 4 cards to get as close to 21 as possible. Whoever is closest to 21 wins");
break;
default:
break;
}




int balance = 300;

System.out.println("Your bank balance is $" + balance);
//user places a bet
System.out.println("Place a bet");

int bet = input.nextInt();
System.out.println("You placed a bet of " + bet);

//this is the AI
AiCard1 = Math.random() * 12 + 1;
AiCard1 = Math.round(AiCard1);
AiCard2 = Math.random() + 12 + 1;
AiCard2 = Math.round(AiCard2);
double AiTotal2cards = AiCard1 + AiCard2;
if(AiTotal2cards < 15) {
AiCard3 = Math.random() * 12 + 1;
AiCard3 = Math.round(AiCard3);
AiTotalcard3 = AiCard1 + AiCard2 + AiCard3;
}



card1 = Math.random() * 12 + 1;
card1 = Math.round(card1);
System.out.println("Your first card was a " + card1);
System.out.println();

card2 = Math.random() * 10 + 1;
card2 = Math.round(card2);

System.out.println("Your second card was a " + card2);
System.out.println();

System.out.println("Your cards add up to " + (card1 + card2));
System.out.println("Do you want to add another card?");
String answer = input.next();
here:
if(answer.equals(Commands.yes)) {

card3 = Math.random() * 12 + 1;
card3 = Math.round(card3);
System.out.println("Your third card was a " + card3);
System.out.println("Your cards add up to " + (card1 + card2 + card3));
total = card1 + card2 + card3;

if(total > 21) {
System.out.println("You lose");
break here;
}
System.out.println("Do you want to add another card?");


String answer1 = input.next();
if(answer1.equals(Commands.no)){
break here;
}
if(answer1.equals(Commands.yes)) {

card4 = Math.random() * 12 + 1;
card4 = Math.round(card4);
System.out.println("Your fourth card was a " + card4);
System.out.println("Your cards add up to " + (card1 + card2 + card3 + card4));
total2 = card1 + card2 + card3 + card4;

if(total2 > 21) {
System.out.println("You lose");
break here;
}
break here;
}
}
System.out.println("Your total cards were " + total2);
System.out.println("AI total cards were " + AiTotalcard3);
input.close();

}
}


When you run the program I was hoping that the variables at the top would have new numbers saved into them when the user went through the program. Is there a better way to do this because the console output is always 0 for the total.

Answer

First lets look at AiTotalcard3:

It shouldn't always output 0 for that, more precisely it will output 0 in the case where AiCard1 + AiCard2 >= 15, which probably happens a lot.

It makes sense if you trace through your actual code. The first thing that happens is those initializers:

double AiCard1 = 0;
double AiCard2 = 0;
double AiCard3 = 0;
double AiTotalcard3 = AiCard1 + AiCard2 + AiCard3;

So AiTotalcard3 is initially set to 0. It will remain 0 until you change it, which happens here:

AiCard1 = Math.random() * 12 + 1;
AiCard1 = Math.round(AiCard1);
AiCard2 = Math.random() + 12 + 1;
AiCard2 = Math.round(AiCard2);
double AiTotal2cards = AiCard1 + AiCard2;
if(AiTotal2cards < 15) {
    AiCard3 = Math.random() * 12 + 1;
    AiCard3 = Math.round(AiCard3);
    AiTotalcard3 = AiCard1 + AiCard2 + AiCard3;
}

But that only happens if the first two cards add up to less than 15. After that you never touch AiTotalcard3 again.

Your logic can be greatly simplified all around, and also it makes a lot more sense to use int instead of double for the cards, but ignoring all that and sticking to your current style, you could fix this by making sure all paths set AiTotalcard3, for example (again not the cleanest logic but just sticking to your pattern):

if(AiTotal2cards < 15) {
    AiCard3 = Math.random() * 12 + 1;
    AiCard3 = Math.round(AiCard3);
    AiTotalcard3 = AiCard1 + AiCard2 + AiCard3;
} else {
    AiTotalcard3 = AiCard1 + AiCard2; // be sure to always set it
}

Now, with all that in mind, you have a similar problem with total2: Not all of your execution paths set the value of total2. It works if you make it up to the 4th card but prior to that you never change it from its initial value of 0. So you're just going to have to go through this with a fine-toothed comb.

One of your fundamental issues is for some reason you're using different variables all over the place for the first 2 cards vs. all the cards. If you just used a single aiTotal and total to accumulate the card totals as you go, you'd avoid most of these issues.

Comments