Project Number 3 and Blackjack
Code
/// Name: Brendan Baird
/// Period: 6
/// Program Name: Blackjack
/// File Name: Blackjack.java
/// Date Finished: 12/14/2015
import java.util.Scanner;
import java.util.Random;
public class Blackjack
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
Random r = new Random();
int playerHand1 = 2 + r.nextInt(9);
int playerHand2 = 2 + r.nextInt(9);
int dealerHand1 = 2 + r.nextInt(9);
int dealerHand2 = 2 + r.nextInt(9);
String choice;
int playerHit;
int dealerHit;
int playerTotal = playerHand1 + playerHand2;
int dealerTotal = dealerHand1 + dealerHand2;
System.out.println("Welcome to Brendan's Blacjack Program!");
System.out.println();
System.out.println("You get a " + playerHand1 + " and a " + playerHand2 + ".");
System.out.println("Your total is " + playerTotal + ".");
System.out.println();
if ( playerTotal <= 21 )
{
System.out.println("The dealer has a " + dealerHand1 + " showing, and a hidden card.");
System.out.println("Her total is hidden, too.");
System.out.println();
do
{
System.out.print("Would you like to \"hit\" or \"stay\"? ");
choice = kb.next();
if (choice.equals("hit"))
{
playerHit = 2 + r.nextInt(9);
playerTotal = playerTotal + playerHit;
System.out.println("You drew a " + playerHit + ".");
System.out.println("Your total is " + playerTotal + ".");
System.out.println();
}
} while (!choice.equals("stay") && playerTotal <= 21);
System.out.println("Okay, dealer's turn.");
System.out.println("Her hidden card was a " + dealerHand2 + ".");
System.out.println("Her total was " + dealerTotal + ".");
System.out.println();
if (dealerTotal <= 21)
{
while (dealerTotal <= 16)
{
dealerHit = 2 + r.nextInt(9);
dealerTotal = dealerTotal + dealerHit;
System.out.println("Dealer chooses to hit.");
System.out.println("She draws a " + dealerHit + ".");
System.out.println("Her total is " + dealerTotal + ".");
System.out.println();
}
if (dealerTotal <= 21)
{
System.out.println("Dealer stays.");
System.out.println();
}
}
}
System.out.println("Dealer total is " + dealerTotal + ".");
System.out.println("Your total is " + playerTotal + ".");
System.out.println();
if (playerTotal > dealerTotal && playerTotal <= 21)
System.out.println("YOU WIN!");
else if (playerTotal < dealerTotal && dealerTotal <= 21)
System.out.println("DEALER WINS!");
else if (playerTotal == dealerTotal && playerTotal <= 21)
System.out.println("YOU TIE! DEALER WINS!");
else if (playerTotal > 21 && dealerTotal > 21)
System.out.println("YOU BOTH BUST!");
else if (playerTotal > 21)
System.out.println("YOU BUST!");
else if (dealerTotal > 21)
System.out.println("DEALER BUSTS!");
}
}
Picture of the output