急求用java编写一个程序使五张扑克牌是同花顺
发布网友
发布时间:2022-04-26 09:08
我来回答
共1个回答
热心网友
时间:2022-06-26 10:04
public class TestStraightFlush {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Card[] cards = new Card[] {
new Card("Spade", 5),
new Card("Spade", 6),
new Card("Spade", 7),
new Card("Spade", 8),
new Card("Spade", 9)
};
System.out.println(isSF(cards)?"是同花顺":"不是同花顺");
}
// 判断是否同花顺
static boolean isSF(Card[] cards) {
// 判断花色
String suit = cards[0].getSuit();
int min = cards[0].getNum();
int max = cards[0].getNum();
for (int i = 1; i < cards.length; i++) {
// 有花色异常则返回false
if (!suit.equals(cards[i].getSuit()))
return false;
//
int num = cards[i].getNum();
if (num > max)
max = num;
else if (num < min)
min = num;
// 最后一张牌,确定max和min是所有牌的最大和最小
if (i == cards.length - 1)
//最大比最小大4,说明是同花顺
return (max - min) == 4;
}
return true;
}
}
// 扑克牌类
class Card {
String suit;// 花色
int num;// 大小
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
this.suit = suit;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public Card(String suit, int num) {
super();
this.suit = suit;
//1按照14理解
if(num == 1)
this.num = 14;
else
this.num = num;
}
}