Project #3 and Blackjack
Code
/// Name: Lauren Baird
/// Period: 7
/// Program Name: Blackjack
/// File Name: p3.java
/// Date Finished: 12/08/2015
import java.util.Scanner;
import java.util.Random;
public class p3
{
public static void main( String[] args )
{
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
System.out.println();
int p1 = 2 + r.nextInt(10);
int p2 = 2 + r.nextInt(10);
int d1 = 2 + r.nextInt(10);
int d2 = 2 + r.nextInt(10);
int pt = p1 + p2;
int dt = d1 + d2;
// the letter 'p' stands for 'player'
// the letter 'd' stands for 'dealer'
// the numbers that follow the letter p/d represents the card
// 'pt' stands for 'players total'
// 'dt' stands for 'dealers total'
System.out.println( "Welcome to the game of blackjack!" );
System.out.println();
System.out.println( "You drew " + p1 + " and " + p2 + "." );
System.out.println( "Your total is " + pt + "." );
System.out.println();
System.out.println( "The dealer has " + d1 + " showing, and a hidden card." );
System.out.println( "You do not know the dealers total." );
System.out.println();
System.out.print( "Would you like to \"hit\" or \"stay\"? " );
String answer = keyboard.next();
if ( answer.equals("hit") )
{
do
{
int p3 = 2 + r.nextInt(10);
pt = pt + p3;
System.out.println( "You drew a " + p3 + "." );
System.out.println( "Your total is " + pt + "." );
if ( pt >= 22 )
{
System.out.println();
System.out.println( "You have gone over 21! The dealer has won." );
}
else
{
System.out.println();
System.out.print( "Would you like to \"hit\" or \"stay\"? " );
answer = keyboard.next();
}
} while ( answer.equals("hit") && pt <= 21 );
}
if ( answer.equals("stay") )
{
System.out.println();
System.out.println( "Alright, now it's the dealer's turn." );
System.out.println( "His hidden card was a " + d2 + "." );
System.out.println( "His total was " + dt + "." );
if ( dt > 16 )
{
System.out.println();
System.out.println( "The dealer has decided to stay." );
}
while ( dt <= 16 )
{
int d3 = 2 + r.nextInt(10);
dt = dt + d3;
System.out.println();
System.out.println( "The dealer drew a " + d3 + "." );
System.out.println( "The dealer's total is " + dt + "." );
if ( dt > 21 )
{
System.out.println();
System.out.println( "The dealer has gone over 21! You win!" );
}
if ( dt > 16 && dt <= 22 || dt == 22 )
{
System.out.println();
System.out.println( "The dealer has decided to stay." );
}
}
if ( pt >= dt && pt <= 22 && pt != dt )
{
System.out.println();
System.out.println( "YOU WIN!" );
}
else if ( pt <= dt && dt <= 22 && pt != dt )
{
System.out.println();
System.out.println( "The dealer has won." );
}
else if ( pt == dt )
{
System.out.println();
System.out.println( "It's a tie, the dealer wins." );
}
}
System.out.println();
}
}