dimanche 28 février 2016

Variables being ran generated are displaying int or displaying cards suits (Black Jack program)

When running the program the output is obviously the to string method written to display the cards value and suit. When running the program it outputs the joption message "Would you like to play (Yes or No)" when you select yes it outputs 0 for both values and null for its suit.

// Card.java, BlackJack.java
// Driver class

package blackjack;
import java.util.Random;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class BlackJack {

      public static int userTotals = 0;
      public static int computerTotals = 0;
      public static Random myRan = new Random();

    public static void main(String[] args) 
    {
       int userAnswer;
       userAnswer = JOptionPane.showConfirmDialog(null, "Would you like to play?");

       while (userAnswer == JOptionPane.YES_OPTION)
       {
           drawUserCard();
           drawComputerCard();
           System.out.println("\n");
           userAnswer = JOptionPane.showConfirmDialog(null, "Would you like to play?");
       }

       if (userTotals > computerTotals  && userTotals <= 21)
       {
           JOptionPane.showMessageDialog(null, "User won this round.");
       }
       else if(computerTotals > userTotals && computerTotals <= 21)
       {
           JOptionPane.showMessageDialog(null, "Computer won this round.");
       }
       else if(userTotals == computerTotals && userTotals <= 21 && computerTotals <= 21)
       {
           JOptionPane.showMessageDialog(null, "It's a tie!");
       }
       else{
           JOptionPane.showMessageDialog(null, "No winner.");
       }

    }

    public static void drawUserCard()
    {
        int cardVal = myRan.nextInt(10) + 1;
        Card userCard = new Card("club", cardVal);
        userTotals += userCard.getCardValue();
        System.out.println("User: " + userCard);
        System.out.println("User Totals: " + userTotals);         


    }

    public static void drawComputerCard()
    {
        int cardVal = myRan.nextInt(10) + 1;
        Card computerCard = new Card("spades", cardVal);
        computerTotals += computerCard.getCardValue();
        System.out.println("Computer: " + computerCard);
        System.out.println("Computer Totals: " + computerTotals);       
    }

}


// Card.java, BlackJack.java

package blackjack;

public class Card
{
    private String suit;   //Clubs, Diamonds, Hearts, Spade
    private int cardValue; // 1 - 11; 

    public Card (String aSuit, int aCardValue)
    {
        aSuit = suit;
        aCardValue = cardValue;
    }

// Getters and setters

    public String getSuit(){
        return suit;
    }

    public int getCardValue(){
        return cardValue;
    }

    public void setSuit(String aSuit){
        suit = aSuit;
    }

    public void setCardValue(int aCardValue){
        cardValue = aCardValue;
    }

    public String toString(){
        String displayCardContent = "The card drawn is " + cardValue + " of " + suit + ".";
        return displayCardContent;
    }
}




Aucun commentaire:

Enregistrer un commentaire