gpt4 book ai didi

java - 使用 Arrays.sort 对数组元素进行排序

转载 作者:行者123 更新时间:2023-12-01 07:57:14 25 4
gpt4 key购买 nike

无论出于何种原因,我无法对卡片数组中的数组进行排序。我收到错误“Card 无法转换为 java.lang.Comparable”,即使我的 Card 类中有 Card 的compareTo 方法。如果我注释掉 Arrays.sort(hand) 方法,程序将按预期运行,但结果会关闭,因为需要对手牌进行排序以查看元素是否等于获胜的扑克手牌。这是代码:

import java.util.Random;
//implements comparable<Card>
public class Card
{

//array for all of the possible faces
private static String[] faces = {"two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "jack", "queen", "king", "ace"};

//array for all of the possible suits
private static String[] suits = {"clubs", "diamonds", "hearts", "spades"};

//variables used for the faces and suits
private String face;
private String suit;
private int faceValue;

//constructor for card; makes a Two of Clubs for testing purposes
public Card()
{
face = "Two";
suit = "Clubs";
}

//deal method; deals a random card
public void deal()
{
Random generator = new Random();
int thisCard = generator.nextInt(52);

face = faces[thisCard % 13];
suit = suits[thisCard / 13];

faceValue = thisCard %13;
}

public int getFaceValue()
{
return faceValue;
}

public String getFace()
{
return face;
}


public String getSuit()
{
return suit;
}

public String toString()
{
return face + " of " + suit + " ";
}

public int compareTo(Card otherCard)
{
if (this.faceValue < otherCard.faceValue)
return -1;
else if (this.faceValue > otherCard.faceValue)
return 1;
else
return 0;
}

}

public class Poker {
public static void main (String [] args)
{
//variables for winning hands
int threeOfAKinds = 0;
int fullHouse =0;
int fourOfAKinds =0;
int fiveOfAKinds = 0;
int twoOfAKinds = 0;


//create hand of cards
Card [] hand = new Card [5];
for (int i =0; i<5; i++)
hand[i] = new Card();

for (int x = 0; x<100; x++)
{



//deal cards
for (int i =0; i<5; i++)
hand[i].deal();

//print cards unsorted
for (Card die : hand)
System.out.print(die);
System.out.println();

//check for winning hands
//sort to make this easier
Arrays.sort(hand);




//check for five of a kind
if (hand[0].getFace()==hand[4].getFace())
fiveOfAKinds++;
//checks for full house
else if (hand[0].getFace() == hand[1].getFace() && hand[2].getFace() == hand[4].getFace()||
hand[0].getFace() == hand[2].getFace() && hand[3].getFace() == hand[4].getFace())
fullHouse++;
//check for four of a kind
else if (hand[0].getFace() == hand[3].getFace() || hand[1].getFace() == hand[4].getFace())
fourOfAKinds++;
//check for 3 of a kind

else if( hand[0].getFace()==hand[2].getFace()
|| hand[1].getFace()==hand[3].getFace()
|| hand[2].getFace()==hand[4].getFace())
threeOfAKinds ++;

//check for two of a kind
else if (hand[0].getFace()==hand[1].getFace()||
hand[1].getFace()==hand[2].getFace() ||
hand[2].getFace()==hand[3].getFace()||
hand[3].getFace()==hand[4].getFace())
twoOfAKinds++;

//print cards sorted
for (Card die : hand)
System.out.print(die.getFace() + " " + die.getSuit());
System.out.println();
}

//report hands
System.out.println("Five of a Kinds:" + fiveOfAKinds);
System.out.println("4 of a kinds:" + fourOfAKinds);
System.out.println("3 of a kinds:" + threeOfAKinds);
}

}

最佳答案

仅有compareTo是不够的;您还需要实现 Comparable<Card> 接口(interface),例如:

public class Card implements Comparable<Card> {
...
}

此外,您的类(class)最好也实现 boolean equals(Object another)方法的行为就像返回 another instanceof Card && this.compareTo(another) == 0; .

关于java - 使用 Arrays.sort 对数组元素进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28482870/

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