Question

In: Computer Science

----------USING JAVA----------- Your objective is to beat the dealer's hand without going over 21. Cards dealt...

----------USING JAVA-----------

Your objective is to beat the dealer's hand without going over 21.

  • Cards dealt (randomly) are between 1 and 11.
  • You receive 2 cards, are shown the total, and then are asked (in a loop) whether you want another card.
  • You can request as many cards as you like, one at a time, but don't go over 21. (If you go over 21 it should not allow you any more cards and you lose)
  • Determine the dealer's hand by generating a random number between 1 and 11 and adding 10 to it.
  • Compare your hand with the dealer's hand and indicate who wins (dealer wins a draw)

Thank you!

Solutions

Expert Solution

Code is :

import java.util.*;

import java.lang.*;

import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */

class Demo

{

public static void main (String[] args) throws java.lang.Exception

{

  

int card1,card2,card3=0,dcard,tcard;

  

  

System.out.println("Hello.. Welcome to 21");

//Random number object

Random rand = new Random();

Scanner sc= new Scanner(System.in);

  

//selecting card 1

//1 is added to the random number to make it between 1-10

card1 = rand.nextInt(10);

card1 += 1;

  

//selecting card 2

card2 = rand.nextInt(10);

card2 += 1;

  

System.out.println(" Your first card is "+card1);

  

System.out.println(" Your second card is "+card2);

//adding user cards

tcard=card1+card2;

while (tcard<21)

{

//prompting user for another card

System.out.println(" Would you like another card... y/n");

  

String cr = sc.nextLine();

  

if (cr.equals("y"))  

{

//generating next card

card3 = rand.nextInt(10);

card3 += 1;  

System.out.println(" Your next card is "+card3);

tcard += card3;

}

else

{

break;

}

}

//generating dealer hand

dcard = rand.nextInt(10);

dcard += 1;

dcard += 10;

  

System.out.println("Dealer hand: "+dcard+" Your hand: "+tcard);

  

//checking winner

if ((tcard>21)||(tcard==dcard)||(tcard<dcard))  

System.out.println("Dealer wins");

else

System.out.println("You win");  

}

}

Input: y y n

Output:


Related Solutions

You are dealt a hand of five cards from a standard deck of 52 playing cards....
You are dealt a hand of five cards from a standard deck of 52 playing cards. Calculate the probability of the given type of hand. (None of them is a recognized poker hand.) (a) Mixed royal meeting: One of each type or royal card (king, queen, jack, ace) of different suites, and a last non-royal card (neither king, queen, jack or ace). Enter a formula. Examples: C(5,3)C(33,3)/C(14,2) C(5,3)C(33,3)C(4,1)/C(100,100) (b) Red and black double royal wedding: A red king, a red...
You are dealt a hand of five cards from a standard deck of 52 playing cards....
You are dealt a hand of five cards from a standard deck of 52 playing cards. Calculate the probability of the given type of hand. (None of them is a recognized poker hand.) 1. Double royal interracial wedding: Two kings of one color, two queens of the other color, and a last non-royal card (neither king, queen, jack or ace). Solve using a formula. Examples:  C(5,3)C(33,3)/C(14,2) or C(5,3)C(33,3)C(4,1)/C(100,100)
A hand of 13 cards is dealt from a standard deck of 52 playing cards. What...
A hand of 13 cards is dealt from a standard deck of 52 playing cards. What is the probability that it contains more spades (♠) than hearts (♡) given that the hand contains at least two spades?
A poker hand consists of five cards randomly dealt from a standard deck of 52 cards....
A poker hand consists of five cards randomly dealt from a standard deck of 52 cards. The order of the cards does not matter. Determine the following probabilities for a 5-card poker hand. Write your answers in percent form, rounded to 4 decimal places. Determine the probability that exactly 3 of these cards are Aces. Answer:  % Determine the probability that all five of these cards are Spades. Answer:  % Determine the probability that exactly 3 of these cards are face cards....
A poker hand consists of five cards randomly dealt from a standard deck of 52 cards....
A poker hand consists of five cards randomly dealt from a standard deck of 52 cards. The order of the cards does not matter. Determine the following probabilities for a 5-card poker hand. Write your answers in percent form, rounded to 4 decimal places. Determine the probability that exactly 3 of these cards are Aces. Answer: % Determine the probability that all five of these cards are Spades. Answer: % Determine the probability that exactly 3 of these cards are...
A poker hand consists of 5 cards dealt from an ordinary deck of 52 playing cards....
A poker hand consists of 5 cards dealt from an ordinary deck of 52 playing cards. How many different hands are there consisting of four cards of one suit and one card of another​ suit?
A bridge hand consists of 13 randomly-dealt cards from a standard deck of 52 cards. What...
A bridge hand consists of 13 randomly-dealt cards from a standard deck of 52 cards. What is the probability that, in a randomly-dealt bridge hand, there is no card that is a ten or higher? (By ten or higher we mean that a card is either a ten, a jack, a queen, a king, or an ace.)
Two cards will be dealt, one at a time and without replacement, from a standard deck...
Two cards will be dealt, one at a time and without replacement, from a standard deck of 52 playing cards. In a standard deck, there are 4 Jacks. Answer the following: a) What is the probability the second card dealt will be a Jack? P(second card will be Jack) = _______ b) What is the probability both cards dealt will be Jacks? P(both dealt cards will be Jacks) = _______ c) What is the probability neither card dealt will be...
Cards are dealt without replacement from a standard deck. (a) Find the conditional probability that the...
Cards are dealt without replacement from a standard deck. (a) Find the conditional probability that the second card is a club given that the first card is club. (b) Find the conditional probability that the second card is a club given that the second-to- last card is a club. (c) Find the conditional probability that the first card is a club given that none of the next five cards are clubs. (d) Find the expected number of clubs that appear...
A 5-card hand is dealt from a perfectly shuffled deck of playing cards. What is the...
A 5-card hand is dealt from a perfectly shuffled deck of playing cards. What is the probability of each of the following events: a) The hand has at least one club b) The hand has at least two cards with the same rank c) The hand has exactly one club or exactly one spade d) The hand has at least one club or at least one spade
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT