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

标签 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);

}

}

最佳答案

好吧,这都是你自找的。

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();

    }

关于Java 为什么我的程序需要静态变量,它会导致问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21710339/

相关文章:

java - 在 Java 中使用静态变量或方法作为常量数组

java - 静态嵌套类,静态属性访问。脑筋急转弯

java - 如何从 CXF WebService 中排除方法 - 奇怪的行为

java - 为什么java RMI不能通过引用获取返回值

java - ThreadPoolExecutor 仅运行第一个可运行对象

JavaFX - GridPane 与 DatePicker 的奇怪行为

java - 解析 Set-Cookie header 的 firefox 源代码的位置?

Java 在新线程中运行静态方法

Java 从我创建的另一个类访问数组元素

c++ - 通过包含访问函数与声明静态