Java为什么我的程序需要静态变量,是否会导致问题?

时间:2014-02-11 18:40:51

标签: java static

我正在尝试使用我学到的东西创造一个二十一点游戏,但我似乎遇到了问题。看起来我在这个项目中所做的一切都要求我使用静态变量,但是当我这样做时,我得不到正确的答案。是什么让java要求变量是静态的,即使你不想使用它们,这也是我的结果总是错误的原因?即尽管每次调用.remove(0),你和经销商都有同一只手。

Card Class:
package cardGames;

import java.util.ArrayList;

public class Card {

private final Rank rank;
private final Suit suit;

private static final ArrayList<Card> deck = new ArrayList<Card>();

public enum Rank {

    Two(2), Three(3), Four(4), Five(5), Six(6), Seven(7), Eight(8), Nine(9), Ten(10), Jack(10), Queen(10), King(10), Ace(11);       

    private int rankNum;

    Rank(int value) {

        this.rankNum = value;

    }

    public int getRankIDNum() {

        return this.rankNum;

    }

}

public enum Suit {

    Clubs(0), Diamonds(1), Hearts(2), Spades(3);

    private int suitNum;

    Suit(int value) {

        this.suitNum = value;

    }

    public int getSuitIDNum() {

        return this.suitNum;

    }

}

public Card(Rank rank, Suit suit) {

    this.rank = rank;
    this.suit = suit;

}

static {

    for(Suit suit: Suit.values()) {

        for(Rank rank: Rank.values()) {

            deck.add(new Card(rank, suit));

        }       

    }       

}

public Rank getRank() {

    return this.rank;

}

public Suit getSuit() {

    return this.suit;

}

public static ArrayList<Card> createDeck() {

    return new ArrayList<Card>(deck);

}

@Override
public String toString() {

    return rank + " of " + suit;

}   

}

这是我游戏的课程。

二十一点类:

package cardGames;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Blackjack {

static ArrayList<Card> deck;
static ArrayList<Card> hand = new ArrayList<Card>();
static ArrayList<Card> dealer = new ArrayList<Card>();

private static int value = 0;
private static int dealerValue = 0;
private static int choice;

private static Scanner play = new Scanner(System.in);

public static void main(String[] args) {

    deck = Card.createDeck();
    Collections.shuffle(deck);
    Collections.shuffle(deck);

    playBlackjack();

}

public static void playBlackjack() {

    dealCards();
    showHand();

    showDealer();

    System.out.println("\nThe value of your hand is: " + getValue(hand) + ". What would you like to do?"
            + "\n 1. Hit \t2. Stay \t3. Quit");

    while(play.hasNextInt()) {

        choice = play.nextInt();

        switch(choice) {

        case 1:
            hit();
            break;

        case 2:
            stay();
            break;

        case 3:
            System.exit(0);
            break;

        default:
            System.out.println("That was not a valid choice, please choose again.");

        }

    }

}

public static void dealCards() {

    hand.add(deck.get(0));
    deck.remove(0);
    hand.add(deck.get(0));
    deck.remove(0);
    dealer.add(deck.get(0));
    deck.remove(0);
    dealer.add(deck.get(0));
    deck.remove(0);

}

public static void showHand() {

    for(Card card: hand) {

        System.out.println(card.toString());

    }

}

public static int getValue(ArrayList<Card> hand) {

    for(Card card: hand) {

        value += card.getRank().getRankIDNum();

    }

    return value;

}

public static void hit() {

    hand.add(deck.get(0));
    deck.remove(0);

    if(getValue(hand) > 21) {

        System.out.println("You have busted with a score of " + getValue(hand) + "! Game over.");
        System.exit(0);

    } else {

        showHand();
        System.out.println("The current value of your hand is: " + value + ".");

    }

}

public static void stay() {

    System.out.println("You are choosing to stay with a score of " + getValue(hand) + ". Let's see what the dealer gets.\n");

    dealer();

}

public static void dealer() {

    showDealer();

    while(getDealer(dealer) > 17) {

        System.out.println("The dealer is currently sitting at " + getDealer(dealer) + "."
                + "\nThe dealer draws a :" + deck.get(0).toString());

        hitDealer();
        showDealer();

    }

    if(getDealer(dealer) >= 17) {

        System.out.println("The dealer is staying with his " + getDealer(dealer) + ".\n");

        System.out.println("Your Hand: \n");
        showHand();

        System.out.println("\nDealer's Hand: \n");
        showDealer();

        if(getDealer(dealer) > getValue(hand)) {

            System.out.println("They always say, 'Never bet against the house.");

        } else if(getDealer(dealer) == getValue(hand)) {

            System.out.println("It's a push, you have the same score as the dealer.");

        } else {

            System.out.println("Your " + getValue(hand) + " beat the dealer's " + getDealer(dealer) + ". Congratulations!");

        }

        System.out.println("Would you like to play again? (1. Yes \t2. No)");

        choice = play.nextInt();

        switch(choice) {

        case 1:
            new Blackjack();
            break;

        case 2:
            System.out.println("\nThanks for playing.");
            System.exit(0);
            break;

        default: 
            System.out.println("That was unintelligible, you must have had a little too much Chardonnay. We're calling you a cab.");
            System.exit(0);

        }
    }   

}

public static void showDealer() {

    for(Card card: hand) {

        System.out.println(card.toString());

    }

    System.out.println("\n");

}

public static int getDealer(ArrayList<Card> dealer) {

    for(Card card: dealer) {

        dealerValue += card.getRank().getRankIDNum();

    }

    return dealerValue;

}

public static void hitDealer() {

    dealer.add(deck.get(0));
    deck.remove(0);

}

}

2 个答案:

答案 0 :(得分:1)

嗯,你带来了自己。

public static void main(String[] args) {

    deck = Card.createDeck();
    Collections.shuffle(deck);
    Collections.shuffle(deck);

    playBlackjack();

}

createDeck()方法。它需要是静态的吗? playBlackjack()方法。它需要是静态的吗?您需要使用new运算符处理对象。 它要求你让一切都是静态的,因为你不使用对象。主要方法应该是这样的。

    public static void main(String[] args) {
        Card cardDeck = new Card();
         ....  //  your code here     
        card.playBlackjack();

    }

答案 1 :(得分:0)

您正在静态主方法中执行所有逻辑,这就是为什么它要求所有变量都是静态的。

在不同的类中抽象出二十一点的整个逻辑,并使用该类实例完成所有必需的任务。