Assignemnt #BlackjackProject
Code
/// name: Leila Ordukhani
/// period: 6
/// program name: Seventy-Seventh Program
/// file name: BlackjackProject.java
/// date finished: 2/25/16
import java.util.Random;
import java.util.Scanner;
public class BlackjackProject
{
public static void main( String[] args )
{
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
int x = 2 + r.nextInt(9);
int y = 2 + r.nextInt(9);
int a = 2 + r.nextInt(9);
int b = 2 + r.nextInt(9);
String choose;
int phit;
int dhit;
int total = x + y;
int dtotal = a + b;
System.out.println("Welcome to Blackjack");
System.out.println();
System.out.println("You get a " + x + " and a " + y + ".");
System.out.println("Your total is " + total + ".");
System.out.println();
if ( total <= 21 )
{
System.out.println("The dealer has a " + a + " 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? ");
choose = keyboard.next();
if (choose.equals("hit"))
{
phit = 2 + r.nextInt(9);
total = total + phit;
System.out.println("You drew a " + phit + ".");
System.out.println("Your total is " + total + ".");
System.out.println();
}
} while (!choose.equals("stay") && total <= 21);
System.out.println("Okay, dealer's turn.");
System.out.println("Her hidden card was a " + b + ".");
System.out.println("Her total was " + dtotal + ".");
System.out.println();
if (dtotal <= 21)
{
while (dtotal <= 16)
{
dhit = 2 + r.nextInt(9);
dtotal = dtotal + dhit;
System.out.println("Dealer chooses to hit.");
System.out.println("She draws a " + dhit + ".");
System.out.println("Her total is " + dtotal + ".");
System.out.println();
}
if (dtotal <= 21)
{
System.out.println("Dealer stays.");
System.out.println();
}
}
}
System.out.println("Dealer total is " + dtotal + ".");
System.out.println("Your total is " + total + ".");
System.out.println();
if (total > dtotal && total <= 21)
System.out.println("You win");
else if (total < dtotal && dtotal <= 21)
System.out.println("Dealer wins");
else if (total == dtotal && total <= 21)
System.out.println("You tied dealer wins");
else if (total > 21 && dtotal > 21)
System.out.println("You both lost");
else if (total > 21)
System.out.println("You lost");
else if (dtotal > 21)
System.out.println("Dealer lost");
}
}
Picture of the output