Blackjack.java - import java.util class Blackjack public...

This preview shows page 1 - 2 out of 2 pages.

import java.util.*; class Blackjack{ public static void main(String args[]){ Scanner scan=new Scanner(System.in); boolean newGame=true; int numberOfGames=1; int selection; // pWins = players wins dWins= dealer wins int matches=0, pWins=0, dWins=0, tie=0; int player=0,dealer; P1Random randomGen =new P1Random(); int selectionNumber; while(true){ if(newGame){ System.out.println("START GAME #"+numberOfGames); ++numberOfGames; newGame=false; player=0; } System.out.println("1. Get another card"); System.out.println("2. Hold card\n3. Print statistics\n4. Exit"); selection=scan.nextInt(); switch(selection){ case 1: selectionNumber=randomGen.nextInt(13)+1; player+=getValue(selectionNumber); if(player==21){ System.out.println("BLACKJACK! You win!"); newGame =true; matches++; pWins++; } else if(player>21){ System.out.println("You exceeded 21! You lose."); newGame =true; matches++; dWins++; } break; case 2: dealer= randomGen.nextInt(11)+16; System.out.println("Dealer hold="+dealer); if(dealer>21 || player>dealer){ System.out.println("You win!"); newGame=true; matches++; pWins++; } else if(player<dealer){ System.out.println("Dealer wins!");

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture