Project #3

Code

   
    /// Name: Afaf Nabeeha
    /// Period: 7
    /// Program Name: Blackjack
    /// File Name: Project3.java
    /// Date Finished: 3/8/2016 public class Project3

        import java.util.Scanner;
        import java.util.Random;
        public class Project3  
        {
            public static void main( String[] args )
            {
                Scanner keyboard = new Scanner(System.in);
                Random r = new Random();
                String decision;
                int dealer, player, pDraw1, pDraw2, dDraw1, dDraw2, pTotal, dTotal, final;
                
                System.out.println( "Welcome to Afaf's blackjack program!" );
                pDraw1 = 3 + r.nextInt(9);
                pDraw2 = 3 + r.nextInt(9);
                dDraw1 = 3 + r.nextInt(9);
                dDraw2 = 3 + r.nextInt(9);
                dTotal = dDraw1 + dDraw2;
                pTotal = pDraw1 + pDraw2;
                
                System.out.println( "You get a " + pDraw1 + " and a " + pDraw2 + "." );
                System.out.println( "Your total is " + pTotal + "." );
                System.out.println();
                System.out.println( "The dealer has a " + dDraw1 + " showing, and a hidden card. " );
                System.out.println( "His total is hidden, too." );
                final = 0;
                
                while ( final != 2 )
                {
                    System.out.print( "Would you like to \"hit\" or \"stay\"? ");
                    decision = keyboard.next();
                    if ( decision.equals("hit") )
                    {
                        pDraw1 = 3 +r.nextInt(9);
                        pTotal = pTotal + pDraw1;
                        System.out.println( "You drew a " + pDraw1 + "." );
                        System.out.println( "Your total is " + pTotal + "." );
                        if ( pTotal > 21 )
                        {
                            final = 2;
                        }
                    }
                    else if ( decision.equals("stay") )
                    {
                        System.out.println( "Okay, dealer's turn. " );
                        System.out.println( "His hidden card was a " + dDraw2 + "." );
                        System.out.println( "His total was " + dTotal + "." );
                        final = 1 + r.nextInt(2);
                        while ( final != 2 )
                        {
                            System.out.println( "Dealer chooses to hit. " );
                            dDraw1 = 3 + r.nextInt(9);
                            dTotal = dDraw1 + dTotal;
                            System.out.println( "He draws a " + dDraw1 + "." );
                            System.out.println( "His total is " + dTotal + "." );
                            if ( dTotal > 21 )
                            {
                                final = 2;
                            }
                        }
                    }
                }
                if ( pTotal > 21 )
                {
                    System.out.println( "You bust. You lose... " );
                }
                else if ( dTotal > 21 )
                {
                    System.out.println( "Dealer busts. You win! " );
                }
                else if ( pTotal > dTotal  )
                {
                    System.out.println( "Dealer stays. " );
                    System.out.println( "Your total is " + pTotal + ". " );
                    System.out.println( "Dealer total is " + dTotal + ". " );
                    System.out.println( "You win! " );
                }
                else if ( pTotal < dTotal )
                {
                    System.out.println( "Dealer stays. " );
                    System.out.println( "Your total is " + pTotal + "." );
                    System.out.println( "Dealer total is " + dTotal + "." );
                    System.out.println( "You lose..." );
                }
            }
        }
             
    

Picture of the output

Assignment P3