In: Computer Science
I am trying to write a program in Java: Pick 4 cards and display the cards and their total value (Ace = 1, King = 13, Queen = 12, and Jack 11...etc. , BUT you should check that no card is a duplicate... there is only one "Ace of Spades" per deck for example. I need to utilize the displayMenu so the program continues to run for the user without breaking. My program is not running correctly.
import java.util.Scanner;
import java.util.Random;
//======================= class Deal4Cards =======
public class Deal4Cards
{
private static Scanner userIn = new
Scanner(System.in);
private static String[] rank;
private static String[] suit;
//======================= void main() ============
public static void main(String[] args)
{
char userPick;
do
{
userPick = displayMenu();
switch(userPick)
{
case 'D':
{
runGame();
break;
}
case 'E':
{
System.out.println("E is here");
break;
}
case 'Q':
{
System.out.println("\n*****"
+ "\n*****"
+ "\nThank you for playing!"
+ "\n*****"
+ "\n*****");
break;
}
default:
System.out.print("\n\n*** INVALID INPUT - TRY AGAIN!
***\n\n");
}
}while(userPick !=
'Q');
}
//======================= char displayMenu() ==
public static char displayMenu()
{
char choice;
String heading1 =
"****Butler's BOOtastic Game******";
String heading2 =
"*************DEAL 4 CARDS***************";
String menu1 =
"(D)eal 4 cards";
String menu2 =
"(E)valuate the value of dealt cards ";
String menu3 = "(Q)uit
program";
String menu4 = "Choose a
letter: ";
System.out.printf("%s\n"
+ "%s\n"
+ "\t%s\n"
+"\t%s\n"
+ "\t%s\n"
+ "%s",heading1, heading2, menu1,
menu2, menu3, menu4);
choice =
userIn.next().toUpperCase().charAt(0);
return choice;
}
//====================void runGame()========================
private static void runGame()
{
int sum =0;
int cardCount=0;
String[] cards = new
String[4];
while (sum != 24 || cardCount !=
4)
{
if(sum >
24 || cardCount > 3)
{
cardCount=0;
sum=0;
}
int
suitIndex = getRandomSuit();
int
rankIndex = getRandomRank();
cards[cardCount] = getSpecifiedCard(rankIndex, suitIndex);
System.out.println("Points = " + (rankIndex + 1));
sum +=
rankIndex + 1;
cardCount++;
}
for (int deck =0; deck <
cards.length; deck++)
{
System.out.println(cards[deck] + " = " + deck + " points");
}
System.out.println("sum = " +
sum);
}
//========================getSpecifiedCard========================
public static String getSpecifiedCard(int
rankIndex, int suitIndex)
{
return rank[rankIndex] +
" of " + suit[suitIndex];
}
//========================getRandomCard()========================
public static String getRandomCard()
{
int suitIndex =
getRandomSuit();
int rankIndex =
getRandomRank();
return rank[rankIndex] +
" of " + suit[suitIndex];
}
//=======================getRandom()=============================
public static int getRandom()
{
return(int)
(Math.random() * 52);
}
//=======================getRandomSuit()================
public static int getRandomSuit()
{
return getRandom()
/13;
}
//=================getRandomRank()========================
public static int getRandomRank()
{
return getRandom()
%13;
}
import java.util.Random;
public class rough {
public static void main(String args[]) {
String[] numbers = {
"Ace",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"Jack",
"Queen",
"King",
};
String[] suit = { "Hearts", "Spades", "Clubs", "Diamonds" };
String[] result = new String[4];
int card_number, card_suit, total = 0;
boolean flag;
Random rand = new Random();
for (int i = 0; i < 4; i++) {
flag = false;
card_number = rand.nextInt(13);
card_suit = rand.nextInt(4);
String temp = numbers[card_number] + " of " + suit[card_suit];
for (int j = 0; j < i + 1; j++) {
if (temp.equals(result[j])) flag = true;
}
if (flag == true) i--; else {
result[i] = temp;
total += card_number + 1;
}
System.out.println(result[i]);
}
System.out.println("Total value is " + total);
}
}
//SAMPLE OUTPUT:
******************************************************************************************
PLEASE LIKE IT RAISE YOUR THUMBS UP
IF YOU ARE HAVING ANY DOUBT FEEL FREE TO ASK IN COMMENT
SECTION
******************************************************************************************