import java.util.*;
public class Pooo {
public static void main(String[] args) {
int card1;
int card2;
int card3;
int card4;
int card5;
int numSuit1;
int numSuit2;
int numSuit3;
int numSuit4;
int numSuit5;
int temp;
int winner = 0;
int counter = 0;
int idcounter = 0;
int frcounter = 0;
String YN = "EMP";
String Finish = "";
String Selection = "EMP";
String Selection1 = "EMP";
String Selection2 = "EMP";
String Selection3 = "EMP";
String Selection4 = "EMP";
String Selection5 = "EMP";
String Selection6 = "EMP";
String Selection7 = "EMP";
String Selection8 = "EMP";
String Selection9 = "EMP";
String RES = "";
String Suit1 = "";
String Suit2 = "";
String Suit3 = "";
String Suit4 = "";
String Suit5 = "";
String card1s = "";
String card2s = "";
String card3s = "";
String card4s = "";
String card5s = "";
Scanner scn = new Scanner(System.in);
while ((!YN.equalsIgnoreCase("Exit")) & (winner == 0)) {
counter = counter + 1;
idcounter = 0; // counter for identification
// pick 5 random numbers for cards
card1 = (int) (Math.random() * 13) + 1;
card2 = (int) (Math.random() * 13) + 1;
card3 = (int) (Math.random() * 13) + 1;
card4 = (int) (Math.random() * 13) + 1;
card5 = (int) (Math.random() * 13) + 1;
// Order from smallest to largest
int cards[] = { card1, card2, card3, card4, card5 };
boolean fixed = false;
while (fixed == false) {
fixed = true;
for (int i = 0; i < cards.length - 1; i++) {
if (cards[i] > cards[i + 1]) {
temp = cards[i + 1];
cards[i + 1] = cards[i];
cards[i] = temp;
fixed = false;
}
}
}
// Generating suits for the 5 cards
numSuit1 = (int) (Math.random() * 4) + 1;
numSuit2 = (int) (Math.random() * 4) + 1;
numSuit3 = (int) (Math.random() * 4) + 1;
numSuit4 = (int) (Math.random() * 4) + 1;
numSuit5 = (int) (Math.random() * 4) + 1;
// testing for repeat cards. If repeat, regenerate both cards' suits
boolean regened = false;
while (regened == false) {
regened = true;
while ((cards[0] == cards[1]) & (numSuit1 == numSuit2)) {
numSuit1 = (int) (Math.random() * 4) + 1;
numSuit2 = (int) (Math.random() * 4) + 1;
regened = false;
}
while ((cards[0] == cards[2]) & (numSuit1 == numSuit3)) {
numSuit1 = (int) (Math.random() * 4) + 1;
numSuit3 = (int) (Math.random() * 4) + 1;
regened = false;
}
while ((cards[0] == cards[3]) & (numSuit1 == numSuit4)) {
numSuit1 = (int) (Math.random() * 4) + 1;
numSuit4 = (int) (Math.random() * 4) + 1;
regened = false;
}
while ((cards[0] == cards[4]) & (numSuit1 == numSuit5)) {
numSuit1 = (int) (Math.random() * 4) + 1;
numSuit5 = (int) (Math.random() * 4) + 1;
regened = false;
}
while ((cards[1] == cards[2]) & (numSuit2 == numSuit3)) {
numSuit2 = (int) (Math.random() * 4) + 1;
numSuit3 = (int) (Math.random() * 4) + 1;
regened = false;
}
while ((cards[1] == cards[3]) & (numSuit2 == numSuit4)) {
numSuit2 = (int) (Math.random() * 4) + 1;
numSuit4 = (int) (Math.random() * 4) + 1;
regened = false;
}
while ((cards[1] == cards[4]) & (numSuit2 == numSuit5)) {
numSuit2 = (int) (Math.random() * 4) + 1;
numSuit5 = (int) (Math.random() * 4) + 1;
regened = false;
}
while ((cards[2] == cards[3]) & (numSuit3 == numSuit4)) {
numSuit3 = (int) (Math.random() * 4) + 1;
numSuit4 = (int) (Math.random() * 4) + 1;
regened = false;
}
while ((cards[2] == cards[4]) & (numSuit3 == numSuit5)) {
numSuit3 = (int) (Math.random() * 4) + 1;
numSuit5 = (int) (Math.random() * 4) + 1;
regened = false;
}
while ((cards[3] == cards[4]) & (numSuit4 == numSuit5)) {
numSuit4 = (int) (Math.random() * 4) + 1;
numSuit5 = (int) (Math.random() * 4) + 1;
regened = false;
}
}
// Switching number suits into words
switch (numSuit1) {
case 1:
Suit1 = "Hearts";
break;
case 2:
Suit1 = "Spades";
break;
case 3:
Suit1 = "Diamonds";
break;
case 4:
Suit1 = "Clubs";
break;
default:
Suit1 = "Error Suit1";
}
switch (numSuit2) {
case 1:
Suit2 = "Hearts";
break;
case 2:
Suit2 = "Spades";
break;
case 3:
Suit2 = "Diamonds";
break;
case 4:
Suit2 = "Clubs";
break;
default:
Suit2 = "Error Suit2";
}
switch (numSuit3) {
case 1:
Suit3 = "Hearts";
break;
case 2:
Suit3 = "Spades";
break;
case 3:
Suit3 = "Diamonds";
break;
case 4:
Suit3 = "Clubs";
break;
default:
Suit3 = "Error Suit3";
}
switch (numSuit4) {
case 1:
Suit4 = "Hearts";
break;
case 2:
Suit4 = "Spades";
break;
case 3:
Suit4 = "Diamonds";
break;
case 4:
Suit4 = "Clubs";
break;
default:
Suit4 = "Error Suit4";
}
switch (numSuit5) {
case 1:
Suit5 = "Hearts";
break;
case 2:
Suit5 = "Spades";
break;
case 3:
Suit5 = "Diamonds";
break;
case 4:
Suit5 = "Clubs";
break;
default:
Suit5 = "Error Suit5";
}
// Switching card number into words
switch (cards[0]) {
case 1:
card1s = "Ace";
break;
case 11:
card1s = "Jack";
break;
case 12:
card1s = "Queen";
break;
case 13:
card1s = "King";
break;
default:
card1s = "Error Card1s";
}
switch (cards[1]) {
case 1:
card2s = "Ace";
break;
case 11:
card2s = "Jack";
break;
case 12:
card2s = "Queen";
break;
case 13:
card2s = "King";
break;
default:
card2s = "Error Card2s";
}
switch (cards[2]) {
case 1:
card3s = "Ace";
break;
case 11:
card3s = "Jack";
break;
case 12:
card3s = "Queen";
break;
case 13:
card3s = "King";
break;
default:
card3s = "Error Card3s";
}
switch (cards[3]) {
case 1:
card4s = "Ace";
break;
case 11:
card4s = "Jack";
break;
case 12:
card4s = "Queen";
break;
case 13:
card4s = "King";
break;
default:
card4s = "Error Card4s";
}
switch (cards[4]) {
case 1:
card5s = "Ace";
break;
case 11:
card5s = "Jack";
break;
case 12:
card5s = "Queen";
break;
case 13:
card5s = "King";
break;
default:
card5s = "Error Card5s";
}
// Printing generated cards
if ((cards[0] <= 10) & (cards[0] != 1))
System.out.println("The first card is: " + cards[0] + " of "
+ Suit1);
if ((cards[0] >= 11) | (cards[0] == 1))
System.out
.println("The first card is: " + card1s + " of " + Suit1);
if ((cards[1] <= 10) & (cards[1] != 1))
System.out.println("The second card is: " + cards[1] + " of "
+ Suit2);
if ((cards[1] >= 11) | (cards[1] == 1))
System.out.println("The second card is: " + card2s + " of "
+ Suit2);
if ((cards[2] <= 10) & (cards[2] != 1))
System.out.println("The third card is: " + cards[2] + " of "
+ Suit3);
if ((cards[2] >= 11) | (cards[2] == 1))
System.out
.println("The third card is: " + card3s + " of " + Suit3);
if ((cards[3] <= 10) & (cards[3] != 1))
System.out.println("The fourth card is: " + cards[3] + " of "
+ Suit4);
if ((cards[3] >= 11) | (cards[3] == 1))
System.out.println("The fourth card is: " + card4s + " of "
+ Suit4);
if ((cards[4] <= 10) & (cards[4] != 1))
System.out.println("The fifth card is: " + cards[4] + " of "
+ Suit5);
if ((cards[4] >= 11) | (cards[4] == 1))
System.out
.println("The fifth card is: " + card5s + " of " + Suit5);
System.out.println();
System.out.println();
// Selection1 is part of the selecting which hand is desired program
// The identification program runs only one of the Selections = RF
Selection1 = "RF";
// Identifying Royal Flush
// Enter the while loop if all suits are the same
while ((numSuit1 == numSuit2) & (numSuit1 == numSuit3)
& (numSuit1 == numSuit4) & (numSuit1 == numSuit5)
& (idcounter == 0) & (winner == 0)
& (!Selection1.equalsIgnoreCase("DRF"))
& (!Selection2.equalsIgnoreCase("DRF"))
& (!Selection3.equalsIgnoreCase("DRF"))
& (!Selection4.equalsIgnoreCase("DRF"))
& (!Selection5.equalsIgnoreCase("DRF"))
& (!Selection6.equalsIgnoreCase("DRF"))
& (!Selection7.equalsIgnoreCase("DRF"))
& (!Selection8.equalsIgnoreCase("DRF"))
& (!Selection9.equalsIgnoreCase("DRF")))
// Royal Flush identification
{
System.out.println("RF");
if ((Selection1.equalsIgnoreCase("RF"))
| (Selection2.equalsIgnoreCase("RF"))
| (Selection3.equalsIgnoreCase("RF"))
| (Selection4.equalsIgnoreCase("RF"))
| (Selection5.equalsIgnoreCase("RF"))
| (Selection6.equalsIgnoreCase("RF"))
| (Selection7.equalsIgnoreCase("RF"))
| (Selection8.equalsIgnoreCase("RF"))
| (Selection9.equalsIgnoreCase("RF"))
| (Selection.equalsIgnoreCase("All"))) {
if ((cards[0] == 1) & (cards[1] == 10) & (cards[2] == 11)
& (cards[3] == 12) & (cards[4] == 13) & (winner == 0)
& (numSuit1 == 1)) {
RES = "Hearts Royal Flush";
winner = winner + 1;
idcounter = idcounter + 1;
}
if ((cards[0] == 1) & (cards[1] == 10) & (cards[2] == 11)
& (cards[3] == 12) & (cards[4] == 13) & (winner == 0)
& (numSuit1 == 2)) {
RES = "Spades Royal Flush";
winner = winner + 1;
idcounter = idcounter + 1;
}
if ((cards[0] == 1) & (cards[1] == 10) & (cards[2] == 11)
& (cards[3] == 12) & (cards[4] == 13) & (winner == 0)
& (numSuit1 == 3)) {
RES = "Diamonds Royal Flush";
winner = winner + 1;
idcounter = idcounter + 1;
}
if ((cards[0] == 1) & (cards[1] == 10) & (cards[2] == 11)
& (cards[3] == 12) & (cards[4] == 13) & (winner == 0)
& (numSuit1 == 4)) {
RES = "Clubs Royal Flush";
winner = winner + 1;
idcounter = idcounter + 1;
} else {
idcounter = idcounter + 1;
}
} else {
idcounter = idcounter + 1;
}
}
}
System.out.println("We have a winner!");
System.out.println("The winning hand is: " + RES);
System.out.println("Counter: " + counter);
System.out.println("Press [Enter] to start over, enter [Exit] to quit.");
YN = scn.nextLine();
}
}
这是我的程序的一部分,随机挑选5张牌,然后找到手。这是它用于寻找皇家同花顺的一部分。该程序将运行没有错误,但循环有时停止,而不是总是,在它找到皇家同花顺之前。我做了4次测试运行,其中只有一个发现皇家同花顺@ 49964循环,其余的停止@ 7007,74873和33228循环。
答案 0 :(得分:2)
使用您实施的方法获得具有相同值的5张卡的概率为~1 / 28500。在这种情况下,您的代码会尝试为所有5张卡选择不同的套装,并进入无限while
循环。