Create a program that will randomly pick two cards (ignore the suit), and tell t
ID: 3792067 • Letter: C
Question
Create a program that will randomly pick two cards (ignore the suit), and tell the user what the total is as if they playing blackjack. Here are the rules: Aces are either worth 1 or 11 10's and all face cards are 10 All other cards are their value (for example, a 6 is worth 6) Declare two int variables, one for each of the two cards. Then determine the total value of the two cards as follows: First, consider just the first card scored as above. If it is an Ace, count that as 11. Next, consider the second card also as above, but if it is an Ace, and the total would be over 21, count it as 1, otherwise 11. Output the two cards and the total to the user. To determine a random card, create a random number between 1 and 13. 1 is an Ace, 11 a Jack, 12 a queen, and 13 a king. All other numbers are their face value. When outputting the cards and calculating the total, you will need two multi-way blocks, each with several options (one each for the face cards, one for the ace, and for the rest). Here are some sample outputs: got a Ace and a 2 for a total of 13 You got a King and a 7 for a total 17 You got a 5 and a 7 for a total of 12Explanation / Answer
import java.util.Random;
public class CardTotal {
static Random rn = new Random();
public static int getNumber()
{
int n = rn.nextInt(13) + 1;
return n;
}
public static String getCardName(int n)
{
if (n == 1)
{
return "ACE";
}
else if (n == 11)
{
return "Jack";
}
else if (n == 12)
{
return "Queen";
}
else if (n == 13)
{
return "King";
}
return String.valueOf(n);
}
public static void main(String[] arg)
{
int cardOne, cardTwo;
String cardOneName;
String cardTwoName;
cardOne = getNumber();
cardOneName = getCardName(cardOne);
if (cardOne >= 10)
{
cardOne = 10;
}
if (cardOne == 1)
{
cardOne = 11;
}
cardTwo = getNumber();
cardTwoName = getCardName(cardTwo);
if (cardTwo >= 10)
{
cardTwo = 10;
}
if (cardOne + cardTwo > 21)
{
cardTwo = 1;
}
else
{
cardTwo = 11;
}
int total = cardOne + cardTwo;
System.out.println("You got a " + cardOneName + " and a " + cardTwoName
+ " for a total of " + total);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.