In this program for blackjack in my card class, it says illegal start of express
ID: 3662735 • Letter: I
Question
In this program for blackjack in my card class, it says illegal start of expression, how do I fix it?
/*BLACKJACK CLASS*/
import java.util.Random;
import java.util.Scanner;
public class Blackjack
{
String type;
int currentcard;
int total = 0; //total in hand
static int dealerTotal = 0; //dealer's total
static int playerTotal = 0; //player's total, static variables allow totals to be compared at the end
Random ran = new Random();
Scanner sc = new Scanner(System.in); //to input whether player wants to hit or stay
public Blackjack(String player)
{
type = player; //whether it's dealer or player
//System.out.println("Welcome to Blackjack!");
Deal();
game();
}
void displayCard()
{
Card potato = new Card(); //calls from card class
int currentcard = potato.drawCard();
String suit = potato.getSuit();
while(currentcard == 0)
{
currentcard = potato.drawCard(); //draws cards
}
switch(currentcard)
{ //cases 11, 12, 13 = user drew a J, Q, or K
case 11:
System.out.println(" Jack " + suit);
total = total + 10;
System.out.println(type + " Current total: " + total); //adds to total score
break; //book says break statements are necessary in switch statements
case 12:
System.out.println(" Queen " + suit);
total = total + 10;
System.out.println(type + " Current Total: " + total);
break;
case 13:
System.out.println(" King " + suit);
total = total + 10;
System.out.println(type + " Current Total: " + total);
break;
case 14:
System.out.println(" Ace " + suit);
total = total + 11;
System.out.println(type + " Current Total: " + total);
break;
default:
System.out.println(" " + currentcard + " " + suit);
total = total + currentcard;
System.out.println(type + " Current Total: " + total);
break;
}
}
void Deal() //draws two cards
{
System.out.println("First card is ");
displayCard();
System.out.println("Second card is ");
displayCard();
}
void game()
{
if(total <= 21)
{
if(!(type.equals("Dealer")))//if it is player's turn
{
System.out.println("Would you like to hit? Y for yes, S for stand.");
if (sc.nextLine().equals("Y"))
{
displayCard();
game();
}
else
{
System.out.println("Your Total: " + total);
playerTotal = total;
}
}
}
//else
//{
// System.out.println(type + " Busted: " + total);
//}
if(type.equals("Dealer")) //if dealer's score is less than 16, dealer will draw card
if((total < 17 && total <=21) || total < playerTotal)
{
displayCard();
game();
}
else //dealer does not save, total is busted
{
if(total <21)
dealerTotal = total;
}
}
void WhoWon()
{
if(dealerTotal == 0)
{
System.out.println("Dealer Busted");
}
if(playerTotal == 0)
{
System.out.println("You Busted");
}
if(dealerTotal > playerTotal)
System.out.println("Dealer Won");
{
if (dealerTotal< playerTotal)
{
System.out.println("You Won");
}
{
if (dealerTotal == playerTotal)
{
System.out.println("Push (Draw)");
}
}
}
}
public static void main(String[] args) //creates object to start game
{
Scanner sc = new Scanner(System.in);
do { System.out.println("Welcome to Blackjack!");
Blackjack player = new Blackjack("Player");
Blackjack dealer = new Blackjack("Dealer");
player.WhoWon();
System.out.println("Would you like to play again? Y/N");
if (sc.nextLine().equals("N") || sc.nextLine().equals("n") )
{
System.out.println("Good bye.");
break;
}
}
while(true);
}
}
/*CARD CLASS*/
import java.util.Random;
import java.util.Random;
public class Card
{
public Card(){
}
public final static int SPADES = 0, // Codes for the 4 suits.
HEARTS = 1,
DIAMONDS = 2,
CLUBS = 3;
public final static int ACE = 1, // Codes for the non-numeric cards.
JACK = 11, // Cards 2 through 10 have their
QUEEN = 12, // numerical values for their codes.
KING = 13;
private final int suit; // The suit of this card, one of the constants
// SPADES, HEARTS, DIAMONDS, CLUBS.
private final int value; // The value of this card, from 1 to 11.
public Card(int cardValue, int cardSuit)
{
value = cardValue;
suit = cardSuit;
}
public String getSuit(int suitGenerator)
{
switch(suitGenerator)
{
case 0: return "of Hearts";
case 1: return "of Diamonds";
case 2: return "of Spades";
case 3: return "of Clubs";
int num = r.nextInt(4);
for(int i = 0; i < 4; i++)
{
if(num == i)
{
return suit[i];
}
}
return "";
}
public int drawCard()
{
Random r = new Random();
return r.nextInt(14);
}
public String toString()
{
return drawCard() + " of " + getSuit();
}
}
Explanation / Answer
/*BLACKJACK CLASS*/
import java.util.Random;
import java.util.Scanner;
public class Blackjack
{
String type;
int currentcard;
int total = 0; //total in hand
static int dealerTotal = 0; //dealer's total
static int playerTotal = 0; //player's total, static variables allow totals to be compared at the end
Random ran = new Random();
Scanner sc = new Scanner(System.in); //to input whether player wants to hit or stay
public Blackjack(String player)
{
type = player; //whether it's dealer or player
//System.out.println("Welcome to Blackjack!");
Deal();
game();
}
void displayCard()
{
Card potato = new Card(); //calls from card class
int currentcard = potato.drawCard();
String suit = potato.getSuit();
while(currentcard == 0)
{
currentcard = potato.drawCard(); //draws cards
}
switch(currentcard)
{ //cases 11, 12, 13 = user drew a J, Q, or K
case 11:
System.out.println(" Jack " + suit);
total = total + 10;
System.out.println(type + " Current total: " + total); //adds to total score
break; //book says break statements are necessary in switch statements
case 12:
System.out.println(" Queen " + suit);
total = total + 10;
System.out.println(type + " Current Total: " + total);
break;
case 13:
System.out.println(" King " + suit);
total = total + 10;
System.out.println(type + " Current Total: " + total);
break;
case 14:
System.out.println(" Ace " + suit);
total = total + 11;
System.out.println(type + " Current Total: " + total);
break;
default:
System.out.println(" " + currentcard + " " + suit);
total = total + currentcard;
System.out.println(type + " Current Total: " + total);
break;
}
}
void Deal() //draws two cards
{
System.out.println("First card is ");
displayCard();
System.out.println("Second card is ");
displayCard();
}
void game()
{
if(total <= 21)
{
if(!(type.equals("Dealer")))//if it is player's turn
{
System.out.println("Would you like to hit? Y for yes, S for stand.");
if (sc.nextLine().equals("Y"))
{
displayCard();
game();
}
else
{
System.out.println("Your Total: " + total);
playerTotal = total;
}
}
}
//else
//{
// System.out.println(type + " Busted: " + total);
//}
if(type.equals("Dealer")) //if dealer's score is less than 16, dealer will draw card
if((total < 17 && total <=21) || total < playerTotal)
{
displayCard();
game();
}
else //dealer does not save, total is busted
{
if(total <21)
dealerTotal = total;
}
}
void WhoWon()
{
if(dealerTotal == 0)
{
System.out.println("Dealer Busted");
}
if(playerTotal == 0)
{
System.out.println("You Busted");
}
if(dealerTotal > playerTotal)
System.out.println("Dealer Won");
{
if (dealerTotal< playerTotal)
{
System.out.println("You Won");
}
{
if (dealerTotal == playerTotal)
{
System.out.println("Push (Draw)");
}
}
}
}
public static void main(String[] args) //creates object to start game
{
Scanner sc = new Scanner(System.in);
do { System.out.println("Welcome to Blackjack!");
Blackjack player = new Blackjack("Player");
Blackjack dealer = new Blackjack("Dealer");
player.WhoWon();
System.out.println("Would you like to play again? Y/N");
if (sc.nextLine().equals("N") || sc.nextLine().equals("n") )
{
System.out.println("Good bye.");
break;
}
}
while(true);
}
}
------------------------------------------------------------------------------------------------------
//Card.java
import java.util.Random;
public class Card {
public final static int SPADES = 0,
HEARTS = 1,
DIAMONDS = 2,
CLUBS = 3;
public final static int ACE = 1,
JACK = 11,
QUEEN = 12,
KING = 13;
//SPADES, HEARTS, DIAMONDS, CLUBS.
private final int suit;
// The value of this card, from 1 to 11.
private final int value;
//Default constructor to set default values
//for value and suit
public Card()
{
value = 0;
suit = 0;
}
//Parameter constructor
public Card(int theValue, int theSuit)
{
value = theValue;
suit = theSuit;
}
public int getValue() {
// Return the int that codes for this card's value.
return value;
}
public String getSuit() {
// Return a String representing the card's suit.
// (If the card's suit is invalid, "??" is returned.)
switch ( suit ) {
case SPADES: return "Spades";
case HEARTS: return "Hearts";
case DIAMONDS: return "Diamonds";
case CLUBS: return "Clubs";
default: return "??";
}
}
public String getValueAsString() {
// Return a String representing the card's value.
// If the card's value is invalid, "??" is returned.
switch ( value ) {
case 1: return "Ace";
case 2: return "2";
case 3: return "3";
case 4: return "4";
case 5: return "5";
case 6: return "6";
case 7: return "7";
case 8: return "8";
case 9: return "9";
case 10: return "10";
case 11: return "Jack";
case 12: return "Queen";
case 13: return "King";
default: return "??";
}
}
public int drawCard()
{
Random r = new Random();
return r.nextInt(14);
}
public String toString()
{
return drawCard() + " of " + getSuit();
}
} // end class Card
----------------------------------------------------------------------------------------------------------------
Sample output:
Welcome to Blackjack!
First card is
10 Spades
Player Current Total: 10
Second card is
7 Spades
Player Current Total: 17
Would you like to hit? Y for yes, S for stand.
y
Your Total: 17
First card is
King Spades
Dealer Current Total: 10
Second card is
Queen Spades
Dealer Current Total: 20
Dealer Won
Would you like to play again? Y/N
y
Y
Welcome to Blackjack!
First card is
2 Spades
Player Current Total: 2
Second card is
3 Spades
Player Current Total: 5
Would you like to hit? Y for yes, S for stand.
S
Your Total: 5
First card is
1 Spades
Dealer Current Total: 1
Second card is
9 Spades
Dealer Current Total: 10
8 Spades
Dealer Current Total: 18
Dealer Won
Would you like to play again? Y/N
Y
Welcome to Blackjack!
First card is
4 Spades
Player Current Total: 4
Second card is
2 Spades
Player Current Total: 6
Would you like to hit? Y for yes, S for stand.
S
Your Total: 6
First card is
King Spades
Dealer Current Total: 10
Second card is
King Spades
Dealer Current Total: 20
Dealer Won
Would you like to play again? Y/N
Y
Welcome to Blackjack!
First card is
Jack Spades
Player Current total: 10
Second card is
3 Spades
Player Current Total: 13
Would you like to hit? Y for yes, S for stand.
Y
2 Spades
Player Current Total: 15
Would you like to hit? Y for yes, S for stand.
Y
5 Spades
Player Current Total: 20
Would you like to hit? Y for yes, S for stand.
Y
King Spades
Player Current Total: 30
First card is
King Spades
Dealer Current Total: 10
Second card is
6 Spades
Dealer Current Total: 16
3 Spades
Dealer Current Total: 19
Dealer Won
Would you like to play again? Y/N
Y
Welcome to Blackjack!
First card is
6 Spades
Player Current Total: 6
Second card is
4 Spades
Player Current Total: 10
Would you like to hit? Y for yes, S for stand.
Y
1 Spades
Player Current Total: 11
Would you like to hit? Y for yes, S for stand.
Y
10 Spades
Player Current Total: 21
Would you like to hit? Y for yes, S for stand.
Y
Queen Spades
Player Current Total: 31
First card is
7 Spades
Dealer Current Total: 7
Second card is
4 Spades
Dealer Current Total: 11
King Spades
Dealer Current Total: 21
Dealer Won
Would you like to play again? Y/N
Y
Welcome to Blackjack!
First card is
10 Spades
Player Current Total: 10
Second card is
King Spades
Player Current Total: 20
Would you like to hit? Y for yes, S for stand.
Y
8 Spades
Player Current Total: 28
First card is
7 Spades
Dealer Current Total: 7
Second card is
7 Spades
Dealer Current Total: 14
6 Spades
Dealer Current Total: 20
Dealer Won
Would you like to play again? Y/N
Y
Welcome to Blackjack!
First card is
Queen Spades
Player Current Total: 10
Second card is
1 Spades
Player Current Total: 11
Would you like to hit? Y for yes, S for stand.
Y
6 Spades
Player Current Total: 17
Would you like to hit? Y for yes, S for stand.
Y
King Spades
Player Current Total: 27
First card is
9 Spades
Dealer Current Total: 9
Second card is
2 Spades
Dealer Current Total: 11
7 Spades
Dealer Current Total: 18
Dealer Won
Would you like to play again? Y/N
Y
Welcome to Blackjack!
First card is
7 Spades
Player Current Total: 7
Second card is
1 Spades
Player Current Total: 8
Would you like to hit? Y for yes, S for stand.
Y
3 Spades
Player Current Total: 11
Would you like to hit? Y for yes, S for stand.
Y
Jack Spades
Player Current total: 21
Would you like to hit? Y for yes, S for stand.
S
Your Total: 21
First card is
4 Spades
Dealer Current Total: 4
Second card is
8 Spades
Dealer Current Total: 12
2 Spades
Dealer Current Total: 14
9 Spades
Dealer Current Total: 23
You Won
Would you like to play again? Y/N
N
Good bye.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.