Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How can I add an intro method to this program so the \"Welcome to blackjack!\" d

ID: 3662206 • Letter: H

Question

How can I add an intro method to this program so the "Welcome to blackjack!" doesn't appear every round? Along with how can I put in a loop that allows the user to play again?

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;
System.out.println("Welcome to Blackjack! The game has now started.");
  

Deal();
game();
  

}
void displayCard()
{
Card potato = new Card();
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);
break;
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);
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);
}
}
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")))
{
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);
// System.exit(0);
}
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)
{
Blackjack player = new Blackjack("Player");
Blackjack dealer = new Blackjack("Dealer");
player.WhoWon();

}

}

/**************Card Class****************/
import java.util.Random;
public class Card
{
public int drawCard()
{
Random r = new Random();
return r.nextInt(14);
}
public String getSuit()
{
String[] suit = new String[4];
suit[0] = "of Hearts";
suit[1] = "of Diamonds";
suit[2] = "of Spades";
suit[3] = "of Clubs";
  
Random r = new Random();
int num = r.nextInt(4);
  
for(int i = 0; i < 4; i++)
{
if(num == i)
{
return suit[i];
}
}
return "";
}
}

Explanation / Answer

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;
       // System.out.println("Welcome to Blackjack! The game has now started.");

       Deal();
       game();

   }

   void displayCard() {
       Card potato = new Card();
       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);
           break;
       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);
       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);
       }
   }

   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"))) {
               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);
           // System.exit(0);
       }
       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) {
       Scanner sc = new Scanner(System.in);
       do {
           System.out
                   .println("Welcome to Blackjack! The game has now started.");
           Blackjack player = new Blackjack("Player");
           Blackjack dealer = new Blackjack("Dealer");
           player.WhoWon();
           System.out
                   .println("Would you like to Continue For Next Game? Y for yes, N for No.");
           if (sc.nextLine().equals("N")) {
               System.out.println("Thank you! ");
               break;
           }

       } while (true);

   }
}

/**************Card Class****************/
import java.util.Random;

public class Card {
   public int drawCard() {
       Random r = new Random();
       return r.nextInt(14);
   }

   public String getSuit() {
       String[] suit = new String[4];
       suit[0] = "of Hearts";
       suit[1] = "of Diamonds";
       suit[2] = "of Spades";
       suit[3] = "of Clubs";

       Random r = new Random();
       int num = r.nextInt(4);

       for (int i = 0; i < 4; i++) {
           if (num == i) {
               return suit[i];
           }
       }
       return "";
   }
}

OUTPUT:

Welcome to Blackjack! The game has now started.
First card is
5 of Hearts
Player Current Total: 5
Second card is
King of Spades
Player Current Total: 15
Ace of Spades
Player Current Total: 26
Player Busted: 26
First card is
7 of Spades
Dealer Current Total: 7
Second card is
9 of Diamonds
Dealer Current Total: 16
Queen of Diamonds
Dealer Current Total: 26
Dealer Busted: 26
Dealer Busted
You Busted
Push (Draw)
Would you like to Continue For Next Game? Y for yes, N for No.
Y
Welcome to Blackjack! The game has now started.
First card is
8 of Clubs
Player Current Total: 8
Second card is
10 of Diamonds
Player Current Total: 18
Would you like to hit? Y for yes, S for stand.
S
Your Total: 18
First card is
King of Diamonds
Dealer Current Total: 10
Ace of Diamonds
Dealer Current Total: 21
Second card is
10 of Diamonds
Dealer Current Total: 31
Dealer Busted: 31
Dealer Busted
You Won
Would you like to Continue For Next Game? Y for yes, N for No.
Y
Welcome to Blackjack! The game has now started.
First card is
10 of Clubs
Player Current Total: 10
Second card is
Queen of Clubs
Player Current Total: 20
Would you like to hit? Y for yes, S for stand.
S
Your Total: 20
First card is
5 of Spades
Dealer Current Total: 5
Second card is
3 of Clubs
Dealer Current Total: 8
7 of Hearts
Dealer Current Total: 15
9 of Spades
Dealer Current Total: 24
Dealer Busted: 24
Dealer Busted
You Won
Would you like to Continue For Next Game? Y for yes, N for No.
N
Thank you!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote