Question

In: Computer Science

I am trying to write a program in Java: Pick 4 cards and display the cards...

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;
    }

Solutions

Expert Solution

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
******************************************************************************************


Related Solutions

I am trying to write a java program that determines if an inputted year is a...
I am trying to write a java program that determines if an inputted year is a leap year or not, but I am not able to use if else statements how would I do it. I don't need the code just advice.
I am trying to write a UDP socket program in which there is a server program...
I am trying to write a UDP socket program in which there is a server program and a client program. This is what I have but I can't figure out where I am going wrong. This is the input and expected output: > gcc udpclient.c –o clientout > gcc udpserver.c –o serverout > ./serverout 52007 ‘to celebrate’ & > ./clientout odin 52007 ‘Today is a good day’ Today is a good day to celebrate //UDP echo client program #include #include...
I am trying to work on LCD display using VHDL. I want to display the word...
I am trying to work on LCD display using VHDL. I want to display the word “TECH” and make it blink on LCD. I need the complete source code (If anyone helps me and the code works I will offer bonus cash for it )
Hello, I am trying to write a C++ program that will do the following: Use the...
Hello, I am trying to write a C++ program that will do the following: Use the STL stack container in a program that reads a string, an arithmetic expression to be exact, one character at a time, and determines if the string has balanced parenthesis – that is, for each left parenthesis there is exactly one matching right parenthesis later in the string.                         Use the following strings to test your program. A+ B - C A * B / (C...
Write a java program that read a line of input as a sentence and display: ...
Write a java program that read a line of input as a sentence and display:  Only the uppercase letters in the sentence.  The sentence, with all lowercase vowels (i.e. “a”, “e”, “i”, “o”, and “u”) replaced by a strike symbol “*”.
I am trying to write code for a program in Visual Studo using Visual Basic programming...
I am trying to write code for a program in Visual Studo using Visual Basic programming language that computes the factorial of an entered number by using a For Loop. My problem is that it keeps re-setting the variable for Factorial. Below is my code if anyone can help. I want it to multiply the given number by that number - 1, then continuing to do so until it hits zero without multiplying by zero. Private Sub BtnCalculate_Click(sender As Object,...
Java Program Use for loop 1.) Write a program to display the multiplication table of a...
Java Program Use for loop 1.) Write a program to display the multiplication table of a given integer. Multiplier and number of terms (multiplicand) must be user's input. Sample output: Enter the Multiplier: 5 Enter the number of terms: 3 5x0=0 5x1=5 5x2=10 5x3=15 2 Create a program that will allow the user to input an integer and display the sum of squares from 1 to n. Example, the sum of squares for 10 is as follows: (do not use...
I am trying to create a function in JAVA that takes in an ArrayList and sorts...
I am trying to create a function in JAVA that takes in an ArrayList and sorts the values by their distance in miles in increasing order. So the closest (or lowest) value would be first. It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below. The code for the main class is not necessary. I am only really asking for the...
I am trying to create a method in JAVA that takes in an ArrayList and sorts...
I am trying to create a method in JAVA that takes in an ArrayList and sorts it by the requested "amenities" that a property has. So if someone wants a "pool" and "gym" it would show all members of the array that contain a "pool" and "gym". It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below. You can edit it...
I am trying to create a makefile for the following program in Unix. The C++ program...
I am trying to create a makefile for the following program in Unix. The C++ program I am trying to run is presented here. I was wondering if you could help me create a makefile for the following C++ file in Unix and show screenshots of the process? I am doing this all in blue on putty and not in Ubuntu, so i don't have the luxury of viewing the files on my computer, or I don't know how to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT