I'm programming Blackjack and all is well, except after I type 'y' to hit again, the program terminates even though total is still less then 21. So my while statement is doing nothing?
package com.github.geodox.blackjack;
import java.util.Random;
import java.util.Scanner;
public class Blackjack
{
private static int total = 0;
private static Random rand = new Random();
private static Scanner scan = new Scanner(System.in);
public static void main(String[] args)
{
int card1 = rand.nextInt(11)+1;
System.out.println(card1);
int card2 = rand.nextInt(11)+1;
System.out.println(card2);
if(card1 == 11 & card2 == 11)
total = 12;
else
total = card1 + card2;
System.out.println(total);
while(total < 21)
{
if(total < 21)
{
System.out.println("Hit? y/n: ");
String hit = scan.nextLine();
if(hit.equals("y"))
{
total += rand.nextInt(11)+1;
hit = null;
}
else if(hit.equals("n"))
{
System.out.println("Your total is: " + total);
break;
}
else
{
System.out.println("Invalid Response");
System.out.println("Your total was: " + total);
break;
}
}
else
System.out.println("BUST! Your total is: " + total);
break;
}
scan.close();
}
}