Project 3
Code
///Name: Cody Swain
///Period: 6
///Project Name: project3
///File Name: project3.java
///Date: 5/9/16
import java.util.Random;
import java.util.Scanner;
public class project3
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
Random r = new Random();
int u1, u2, d1, d2, c, utotal, dtotal;
u1 = 2 + r.nextInt(10);
u2 = 2 + r.nextInt(10);
d1 = 2 + r.nextInt(10);
d2 = 2 + r.nextInt(10);
utotal = u1 + u2;
dtotal = d1 + d2;
System.out.println("Welcome to Cody's Blackjack Program!!!");
System.out.println("You drew a " + u1 + " and a " + u2 + ".");
System.out.println("Your total is " + utotal + ".");
System.out.println("");
System.out.println("The dealer has a " + d1 + " showing, and a hidden card.");
System.out.println("His total is hidden too.");
System.out.println("");
System.out.print("Would you like to 'hit' or 'stay'? ");
String choice = kb.next();
while (choice.equals("hit") && utotal <= 21)
{
c = 2 + r.nextInt(10);
utotal = utotal + c;
System.out.println("You drew a " + c + ".");
System.out.println("Your total is " + utotal + ".");
System.out.println("");
if (utotal <= 21)
{
System.out.print("Would you like to 'hit' or 'stay'? ");
choice = kb.next();
}
else
{
System.out.println("You busted! Dealer wins!");
}
}
if (utotal <= 21)
{
System.out.println("");
System.out.println("Okay, dealer's turn.");
System.out.println("His hidden card is a " + d2 + ".");
System.out.println("His total is " + dtotal + ".");
}
while (utotal <= 21 && dtotal <= 16)
{
c = 2 + r.nextInt(10);
dtotal = dtotal + c;
System.out.println("");
System.out.println("The dealer chooses to hit.");
System.out.println("He draws a " + c + ".");
System.out.println("His total is " + dtotal + ".");
System.out.println("");
}
if (dtotal > 21)
{
System.out.println("The dealer busted. You win!");
}
else if (dtotal <= 21 && utotal <= 21)
{
if (utotal > dtotal)
System.out.println("YOU WIN!");
else if (utotal < dtotal)
System.out.println("DEALER WINS!");
else
System.out.println("IT'S A TIE!");
}
}
}
Picture of the output