-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlackJack.java
74 lines (63 loc) · 2.3 KB
/
BlackJack.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// This program was created to be a card game initially providing the user with two cards
// and then requesting the user to input for either more cards or to cease the game.
// The game's goal is toward achieving the highest value possible without exceeding a limitation.
// This project was completed in Java.
import java.util.Scanner;
import java.util.Random;
public class BlackJack
{
public static void main(String args[])
{
Scanner keyboard = new Scanner(System.in);
Random random = new Random();
char toPlay;
int firstCard;
int secondCard;//first set of cards
int handTotal;
int requestedCard;//hit me card
int anotherCard;//user requests card; 'hit me'
System.out.println("\nWelcome to the game of BlackJack.\n");
System.out.print("Enter yes or no to play: ");
toPlay = keyboard.next().charAt(0);
while(toPlay == 'y')
{
firstCard = random.nextInt(10) + 1;
secondCard = random.nextInt(10) + 1;
System.out.println("\nFirst cards: " + firstCard + "," + secondCard);
handTotal = firstCard + secondCard;
System.out.println("Total: " + handTotal + "\n");
System.out.print("Do you want another card? Enter yes or no: ");
anotherCard = keyboard.next().charAt(0);
if(anotherCard == 'n')
{
System.out.println("\nTotal: " + handTotal + ". Hand Rest.");
}
while(anotherCard == 'y')
{
requestedCard = random.nextInt(10) + 1;
System.out.println("\nCard: " + requestedCard);
handTotal = handTotal + requestedCard;
System.out.println("Total: " + handTotal);
if(handTotal == 21)
{
System.out.println("\nTotal: " + handTotal + ". Winner!!!");
break;
}
if(handTotal > 21)
{
System.out.println("\nTotal: " + handTotal + ". Hand value is greater than 21.");
System.out.println("Bust.");
break;
}
System.out.print("Do you want another card? ");
anotherCard = keyboard.next().charAt(0);
if(anotherCard == 'n')
{
System.out.println("\nTotal: " + handTotal + ". Hand Rest.");
}
}
System.out.print("Enter yes or no to play again: ");
toPlay = keyboard.next().charAt(0);
}
}
}