gpt4 book ai didi

Java - 纸牌游戏输出问题

转载 作者:行者123 更新时间:2023-11-30 08:13:15 25 4
gpt4 key购买 nike

我很困惑为什么我的类(class)没有以正确的格式打印。当四只手都应该由不同的牌组成时,它只会为每只手打印相同的 Card 对象。

最佳答案

一个可运行的示例会很好,但快速阅读后我认为您的 if 语句实际上应该是 if-then-else,例如

//If there is a suit with 3 cards or more, return 0 points.
if ( c >=3 || d >= 3 || s >=3 || h >=3) {
retVal = 0;
}
//If there is a suit with 2 cards, return 1 points.
else if ( c == 2 || d == 2 || s == 2 || h == 2) {
retVal = 1;
}
//If there is a suit with 1 card, return 2 points.
else if ( c == 1 || d == 1 || s == 1 || h == 1) {
retVal = 2;
}
//If there is a suit with 0 cards, return 3 points.
else {
retVal = 3;
}

请注意末尾的 else - 因为您已经涵盖了大于 0 的所有内容,所以不需要在末尾测试 0。

编辑

你的代码可以被清理很多。代码越简洁,就越容易阅读。一些建议:

评论

您有很多注释,但您正在使用这些注释来弥补错误的变量名称!例如,

//Variable to hold number of cards that contain the club suit.
int c = 0;

这很好,但是当您稍后在代码中使用 c 时会怎么样?这不是不言自明的。首先为变量命名有意义的东西会更有用。

int clubs = 0;

枚举

不要使用字符来表示套装,而是使用枚举。它更好地体现了含义,并减少了拼写错误破坏代码的可能性,就像在字符串/字符上进行测试时可能发生的情况一样。

public enum Suit
{
CLUBS,
DIAMONDS,
HEARTS,
SPADES
}

对于每个循环

除非您需要索引变量,否则 foreach 循环更容易阅读。

 for (int i = 0; i < theHand.length; i++) {
...
}

可以重写为

for (Card card : cards) {
...
}

缩小范围

在需要时而不是之前声明变量 - 这会缩小它们的范围,从而减少误用它们的机会。例如,

public int countDistributionPoints() {
//Variable to hold the number of distribution points.
int retVal = 0;

// a whole bunch of stuff that doesn't use retVal

//If there is a suit with 3 cards or more, return 0 points.
if ( c >=3 || d >= 3 || s >=3 || h >=3) {
retVal = 0;
}
//etc

return retVal;
}

您可以通过将 retVal 移动到使用它的位置来缩小它的范围。

public int countDistributionPoints() {
// a whole bunch of stuff that doesn't use retVal

//Variable to hold the number of distribution points.
int retVal = 0;

//If there is a suit with 3 cards or more, return 0 points.
if ( c >=3 || d >= 3 || s >=3 || h >=3) {
retVal = 0;
}
//etc

return retVal;
}

组合起来

重写您的 Hand 类以及一些 switch 语句和重命名的变量即可实现这一点。

public class Hand {
private final Card[] cards;

public Hand(Card[] cards) {
this.cards = cards;
}

/**
* Looks through each Card in the hand array and
* adds its points (if it has any) to a sum.
* @return the sum of the hand
*/
public int countHighCardPoints() {
int points = 0;
for (Card card : cards) {
points += card.getPoints();
}
return points;
}

/**
* Count the number of Cards in each suit:
* a suit with 3 cards or more counts for zero points
* a suit with 2 cards counts one point (this is called a doubleton)
* a suit with 1 card counts 2 points (this is called a singleton)
* a suit with 0 cards counts 3 points (this is called a void)
* @return the sum of the points
*/
public int countDistributionPoints() {
int clubs = 0;
int diamonds = 0;
int spades = 0;
int hearts = 0;

for (Card card : cards) {
switch (card.getSuit()) {
case CLUBS:
clubs++;
break;
case DIAMONDS:
diamonds++;
break;
case SPADES:
spades++;
break;
case HEARTS:
hearts++;
break;
}
}

final int result;
if (clubs >= 3 || diamonds >= 3 || spades >= 3 || hearts >= 3) {
result = 0;
}
else if (clubs == 2 || diamonds == 2 || spades == 2 || hearts == 2) {
result = 1;
}
else if (clubs == 1 || diamonds == 1 || spades == 1 || hearts == 1) {
result = 2;
}
else {
result = 3;
}

return result;
}

public String toString() {
String club = "";
String diamond = "";
String heart = "";
String spade = "";

for (Card card : cards) {
switch (card.getSuit()) {
case CLUBS:
club.append(card.toString().replace(",", " "));
break;
case DIAMONDS:
diamond.append(card.toString().replace(",", " "));
break;
case HEARTS:
heart.append(card.toString().replace(",", " "));
break;
case SPADES:
spade.append(card.toString().replace(",", " "));
break;
}
}

//Concatenates all of the string values of the clubs, diamonds, hearts, and spades into one string.
return club + "\n" + diamond + "\n" + heart + "\n" + spade;
}

}

关于Java - 纸牌游戏输出问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30045058/

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