Java-扑克牌游戏输出问题
我一直在想为什么我的班级没有以正确的格式打印。当四只手中的每一只手都应该由不同的卡组成时,它只是为每只手打印相同的卡对象。一个可运行的示例会很好,但从快速阅读中,我认为您的Java-扑克牌游戏输出问题,java,Java,我一直在想为什么我的班级没有以正确的格式打印。当四只手中的每一只手都应该由不同的卡组成时,它只是为每只手打印相同的卡对象。一个可运行的示例会很好,但从快速阅读中,我认为您的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
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;
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;
}
这很好,但是当您稍后在代码中使用c
时呢?这不是不言自明的。首先有意义地命名变量更有用
int clubs = 0;
枚举
不要对套装使用字符,而是使用枚举。它更好地体现了含义,并减少了打字错误破坏代码的机会,这在测试字符串/字符时可能会发生
public enum Suit
{
CLUBS,
DIAMONDS,
HEARTS,
SPADES
}
对于每个循环
除非您需要索引变量,否则for each循环更易于读取
for (int i = 0; i < theHand.length; i++) {
...
}
缩小范围
在需要时声明变量,而不是在此之前-这减少了变量的范围,从而减少了错误使用它们的机会。比如说,
//Variable to hold number of cards that contain the club suit.
int c = 0;
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;
}
}您需要再次声明对象 classname objectname=新的classname()
在制作双手的东西里面在过去几天里,我看到了这个问题的几个变体。我想桥牌和其他纸牌游戏都有家庭作业。你希望有人读这个问题吗?!你好我不太明白你的意思。