Project 3 and Blackjack
Code
/// Name: David Shkolnikov
/// Period: 6
/// Program Name: Blackjack
/// File Name: Blackjack.java
/// Date Finished: 3/14/2016
import java.util.Scanner;
import java.util.Random;
public class Blackjack
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
Random r = new Random();
System.out.println("Welcome to Dave's blackjack game!");
int card1, card2, dealer1, dealer2, c, playerTotal, dealerTotal;
card1 = 2 + r.nextInt(10);
card2 = 2 + r.nextInt(10);
dealer1 = 2 + r.nextInt(10);
dealer2 = 2 + r.nextInt(10);
playerTotal = card1 + card2;
dealerTotal = dealer1 + dealer2;
String answer;
System.out.println("You get a " + card1 + " and a " + card2 + ".");
System.out.println("Your total is " + playerTotal + ".");
System.out.println("\nThe dealer has a " + dealer1 + " showing, and a hidden card.");
System.out.println("His total is hidden, too.");
do
{
System.out.print("\nWould you like to \"hit\" or \"stay\"? ");
answer = keyboard.next();
if ( answer.equals("hit") && playerTotal <= 21 )
{
c = 2 + r.nextInt(10);
playerTotal = playerTotal + c;
System.out.println("You drew a " + c + ".");
System.out.println("Your total is " + playerTotal + ".");
}
}
while ( answer.equals("hit") && playerTotal <= 21 );
if ( playerTotal > 21 )
System.out.println("\nYou busted! The dealer wins!");
if ( playerTotal <= 21 )
{
System.out.println("\nOkay, dealer's turn.");
System.out.println("His hidden card was a " + dealer2 + ".");
System.out.println("His total is " + dealerTotal + ".");
}
while ( dealerTotal <= 16 && playerTotal <= 21 )
{
c = 2 + r.nextInt(10);
dealerTotal = dealerTotal + c;
System.out.println("\nDealer chooses to hit.");
System.out.println("He draws a " + c + ".");
System.out.println("His total is " + dealerTotal + ".");
}
if ( dealerTotal > 16 && playerTotal <= 21 && dealerTotal <= 21 )
System.out.println("\nDealer stays.");
if ( dealerTotal > 21 )
System.out.println("\nThe dealer busted. YOU WIN!!!");
else if ( dealerTotal <= 21 && playerTotal <= 21 )
{
System.out.println("Dealer total is " + dealerTotal + ".");
System.out.println("Your total is " + playerTotal + ".");
if ( dealerTotal > playerTotal)
System.out.println("The dealer wins.");
else
System.out.println("You win!");
}
}
}
Picture of the output