1个回答
展开全部
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;
}
}
/**
* @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;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询