gpt4 book ai didi

卡程序中的 Java getter 和 setter 问题

转载 作者:行者123 更新时间:2023-12-01 22:11:48 24 4
gpt4 key购买 nike

我正在做一项学校作业,但我遇到了一个我一生都无法解决的问题。

这只是一个简单的“纸牌游戏”作业,我们基本上只是创建一副纸牌,洗牌并分发几张纸牌。

当“发牌”A、J、Q 和 K 牌时,我的问题仍然是从 11-13 打印为整数。我相信问题应该出在 setValue() 下的 Card 类中,但看起来一切正常。我将首先发布该类(class),然后发布其余部分,以防万一您也需要查看该类(class)。

感谢您的帮助!!

附注我试图在书中查找这个问题,但找不到答案...BlackJack 类的部分源代码来自本书前面的章节,我不知道是否有必要拥有 super ();在构造函数那里?我认为只有父类构造函数有参数时才需要这样做?有关它是否必须存在以及如果存在的话它正在做什么的任何帮助。

public class Card {

protected String suit;
protected int value;
protected String rank;
protected final int LOW_VALUE = 1;
protected final int HIGH_VALUE = 13;


public String getRank() {
return rank;
}

public int getValue() {
return value;
}

public String getSuit() {
return suit;
}

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

public void setValue(int val) {
if(val >= LOW_VALUE && val <= HIGH_VALUE) {
value = val;
}
else {
value = LOW_VALUE;
}
if(val == 1) {
rank = "Ace";
}
else if(val == 11) {
rank = "Jack";
}
else if(val == 12) {
rank = "Queen";
}
else if(val == 13) {
rank = "King";
}
else {
rank = Integer.toString(value);
}
}
}

卡牌游戏类

abstract public class CardGameFP {

int suitNum = 1;
int val = 1;
int player1;
int player2;
protected final int DECK_OF_CARDS = 52;
Card fullDeck[] = new Card[DECK_OF_CARDS];
protected final int LOW_VALUE = 1;
protected final int HIGH_VALUE = 13;
protected final int HIGH_SUIT = 4;
protected final int CARDS_IN_SUIT = 13;

public abstract void display();
public abstract void dealCards();

public CardGameFP() {
for(int i = 0; i < fullDeck.length; i++) {
fullDeck[i] = new Card();
if(suitNum == 1) {
fullDeck[i].setSuit("Spades");
}
else if(suitNum == 2) {
fullDeck[i].setSuit("Hearts");
}
else if(suitNum == 3) {
fullDeck[i].setSuit("Diamonds");
}
else {
fullDeck[i].setSuit("Clubs");
}
fullDeck[i].setValue(val);
val++;
if(val > HIGH_VALUE) {
suitNum++;
val = 1;
}
}//end for
}
public void shuffle() {
for(int firstCard = 0; firstCard < DECK_OF_CARDS; firstCard++ ) {
firstCard = ((int)(Math.random() * 500) % DECK_OF_CARDS);
int secondCard = ((int)(Math.random() * 500) % DECK_OF_CARDS);
Card temp = fullDeck[firstCard];
fullDeck[firstCard] = fullDeck[secondCard];
fullDeck[secondCard] = temp;
}
}
}

二十一点级

 public class BlackJack extends CardGameFP{

public BlackJack() {
**super();**
player1 = 2;
player2 = 2;
}

public void display() {
System.out.println("BlackJack");
}
public void dealCards() {
//Player 1
System.out.println("Player 1:");
for(int x = 0; x < player1; x++) {
System.out.println(fullDeck[x].getValue() + " of " + fullDeck[x].getSuit() );
shuffle();
}
//Player 2
System.out.println("\nPlayer 2:");
for(int x = 0; x < player2; x++) {
System.out.println(fullDeck[x].getValue() + " of " + fullDeck[x].getSuit() );
shuffle();
}
}
}

PlayerCardGame

public class PlayCardGame {

public static void main(String[] args) {
Card CG = new Card();
BlackJack BJ = new BlackJack();

BJ.display();
BJ.dealCards();
}
}

最佳答案

好吧,您声明并初始化了 rank 字符串(看起来编码得很好),但您正在打印 value

在二十一点类(class)中,您有:

System.out.println(fullDeck[x].getValue() + "of "+ fullDeck[x].getSuit() );

什么时候你应该有这个:

System.out.println(fullDeck[x].getRank() + "of "+ fullDeck[x].getSuit() );

关于卡程序中的 Java getter 和 setter 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31691781/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com