[英]Java: Poker Hand
所以,我必須使用函數/方法和數組創建一個撲克手程序。
這是我需要的示例輸出:
Enter five numeric cards, no face cards. Use 2 - 9.Card 1: 8 Card 2: 7 Card 3: 8 Card 4: 2 Card 5: 7 Two Pair! Enter five numeric cards, no face cards. Use 2 - 9. Card 1: 4 Card 2: 5 Card 3: 6 Card 4: 8 Card 5: 7 Straight! Enter five numeric cards, no face cards. Use 2 - 9. Card 1: 9 Card 2: 2 Card 3: 3 Card 4: 4 Card 5: 5 High Card!
這是我的代碼(我在確定一個人是否得到一對、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;
}
}
如果需要更多信息,我將非常樂意提供。 謝謝!
我建議您不要對手牌進行排序,而是對手牌的內容進行計數並生成一個計數數組,其中數組的第i
個元素具有值為i
的卡片數量。 然后,您應該能夠弄清楚如何使用該數組來確定它是否是特定類型的手牌。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.