【发布时间】:2014-11-04 15:54:29
【问题描述】:
所以,我必须使用函数/方法和数组创建一个扑克手程序。
这是我需要的示例输出:
输入五张数字卡,没有人脸卡。使用 2 - 9. 卡片 1:8 卡 2:7 卡 3:8 卡 4:2 卡 5:7 两对! 输入五张数字卡,没有人脸卡。使用 2 - 9。 卡 1:4 卡 2:5 卡 3:6 卡 4:8 卡 5:7 直的! 输入五张数字卡,没有人脸卡。使用 2 - 9。 卡 1:9 卡 2:2 卡 3:3 卡 4:4 卡 5:5 高卡!
这是我的代码(我在确定一个是否配对、3 个等方面存在逻辑问题)。它们是方法/功能。所以,如果我能弄清楚如何做其中的 1 或 2 个,那应该是轻而易举的事:
import java.util.Scanner;
public class Assignment4
{
public static void main(String args[])
{
final int LEN = 5;
int[] hand = new int[LEN];
Scanner input = new Scanner(System.in);
//input the hand
System.out.println("Enter five numeric cards, no face cards. Use 2-9.");
for (int index = 0; index < hand.length; index++) {
System.out.print("Card " + (index + 1) + ": ");
hand[index] = input.nextInt();
}
//sort the collection
bubbleSortCards(hand);
//determine players hand type
//flow of evaluation -> checking complex hands first
if (containsFullHouse(hand)) {
System.out.println("Full House!");
} else if (containsStraight(hand)) {
System.out.println("Straight!");
} else if (containsFourOfaKind(hand)) {
System.out.println("Four of a Kind!");
} else if (containsThreeOfaKind(hand)) {
System.out.println("Three of a Kind!");
} else if (containsTwoPair(hand)) {
System.out.println("Two Pair!");
} else if (containsPair(hand)) {
System.out.println("Pair!");
} else
System.out.println("High Card!");
}
这是作业说明中推荐的方式:
public class PokerHand
{
public static void main(String args[])
{
int hand[] = {5, 2, 2, 3, 8};
if (containsAPair(hand)) {
System.out.println("Pair!");
} else {
System.out.println("Not a pair!");
}
}
public static boolean containsAPair(int hand[]) {
// Your code here... don’t return true every time...
return true;
}
}
如果需要更多信息,我将非常乐意提供。谢谢!
【问题讨论】:
-
你的问题是什么
-
那么在
// Your code here... don’t return true every time...的那一点上,你尝试了什么? -
一个附带问题:您应该组织检查,以便它们按价值的降序排列,而不是复杂性。现在,您在检查完葫芦或顺子之后检查四个同类,但四个同类都击败了这两者。