在我的程序中能够向玩家交易5张随机牌,但我想知道如何在我的程序中加入一个套牌洗牌,因为在我的随机化方式中我倾向于最终使用重复的卡
import java.util.Scanner;
public class Prog2d {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int userChoice;
String[] suit = {"Clubs","Diamonds","Hearts","Spades"};
String[] faces = {"2","3","4","5","6","7","8","9","10","Jack","King","Queen","Ace"};
String[][] deck = new String[faces.length][suit.length];
System.out.println("Program 2d, Christian Villa, masc1854");
userChoice = in.nextInt();
while(userChoice > 0)
{
for(int k = 0; k < 5;k++)
{
int i = (int)(Math.random()*suit.length);
int j = (int)(Math.random()*faces.length);
System.out.println(faces[j] + " of " + suit[i]);
}
userChoice--;
System.out.println();
}
}
}
答案 0 :(得分:0)
您可以先使用Collections.shuffle
数组进行随机播放,然后在不使用Math.random
方法的情况下进行迭代:
while(userChoice > 0)
{
Collections.shuffle(Arrays.asList(suit));
Collections.shuffle(Arrays.asList(faces));
for(int k = 0; k > 4;k++)
{
System.out.println(faces[k] + " of " + suit[k]);
}
userChoice--;
System.out.println();
}