Project 3
import java.util.Scanner;
import java.util.Random;
public class BetterBlackjack
{
public static void main ( String[] args )
{
Scanner keyboard = new Scanner(System.in);
Random r = new Random();
int p1 = 1 + r.nextInt(11);
int p2 = 1 + r.nextInt(11);
int d1 = 1 + r.nextInt(11);
int d2 = 1 + r.nextInt(11);
int ptotal = p1 + p2;
int dtotal = d1 + d2;
int pnew, dnew;
String action = "hit";
System.out.println("Welcome to Blackjack\n\nYou drew " +p1+ " and " +p2+ ".\nYour total is " +ptotal+ ".\n\nThe dealer has " +d1+ " showing.");
while (ptotal<22 && action.equals("hit"))
{
System.out.print("Would you like to \"hit\" or \"stay\"? ");
action = keyboard.nextLine();
if (action.equals("hit"))
{
pnew = 1 + r.nextInt(11);
ptotal = ptotal + pnew;
System.out.println("You drew a " +pnew+ ".\nYour total is " +ptotal+ ".\n");
}
}
if (ptotal < 22)
{
System.out.println("\nIt's now the dealer's turn.\nHis hidden card was " +d2+ ".\nHis total is " +dtotal+ ".\n");
while (dtotal < 17)
{
dnew = r.nextInt(11);
dtotal = dtotal + dnew;
System.out.println("The dealer chooses to hit.\nHe draws " +dnew+ ".\nHis total is " +dtotal+ ".\n");
}
if (dtotal < 22)
System.out.println("The dealer stays.\n");
}
System.out.println("The dealer's total is " +dtotal+ ".\nYour total is " +ptotal+ ".\n");
if (ptotal > 21)
System.out.println("You lose.");
else if (dtotal > 21)
System.out.println("You win!");
else if (ptotal <= dtotal)
System.out.println("You lose.");
else
System.out.println("You win!");
}
}