Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- //public static void main(String[] args)
- //{
- // double number = Math.ceil(Math.random()*10);
- // System.out.println(number);
- //}
- public class MyFirstClass
- {
- public static double playerDraw()
- {
- double player = Math.ceil(Math.random()*(10-2)+2); // card range Math.random()*(max-min)+min
- System.out.printf(">>Player draws card# %.0f\n", player);
- return player;
- }
- public static double dealerDraw()
- {
- double dealer = Math.ceil(Math.random()*(10-2)+2);
- System.out.printf(">>Dealer draws card# %.0f\n", dealer);
- return dealer;
- }
- public static void main(String[] args)
- {
- double p = 0;
- double d = 0;
- for(int i=0; i<=21;i++) // the game starts
- {
- // p and d are used to keep track of the total for each player
- p = p + playerDraw();
- d = d + dealerDraw();
- System.out.printf("Player Card Total=%.0f\n", p);
- System.out.printf("Dealer Card Total=%.0f\n", d);
- if(p==21 || d==21) // check if we have a winner (blackjack)
- {
- if(p==21)
- {
- System.out.println("Blackjack! Player wins!");
- System.exit(0);
- }
- if(d==21)
- {
- System.out.println("Blackjack! Dealer wins! Player loses!");
- System.exit(0);
- }
- }
- if(p>21 || d>21) // check if either is over 21
- {
- if(p>21)
- {
- System.out.printf("Player total: %.0f, Dealer total: %.0f; Player loses!", p, d);
- System.exit(0);
- }
- if(d>21)
- {
- System.out.printf("Player total: %.0f, Dealer total: %.0f; Player wins!, dealer over 21", p, d);
- System.exit(0);
- }
- }
- if((p>=18 || p==20)&&(d>=18 || d==20))//closest to 21
- {
- if(p>d)
- {
- System.out.printf("Player total: %.0f, Dealer total: %.0f; Player wins!", p, d);
- System.exit(0);
- }
- if(d>p)
- {
- System.out.printf("Player total: %.0f, Dealer total: %.0f; Dealer wins!", p, d);
- System.exit(0);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement