CompSci 클래스에는 2 차원, 6 행 8 열 배열로 카드 덱을 인쇄해야하는 과제가 있습니다. 각 "카드"는 기본적으로 임의로 생성 된 번호 (1-12)와 무작위로 선택된 수트 (Diamonds, Hearts, Spades, & 클럽)입니다. 어떤 카드도 배열의 아무 곳에서나 반복 될 수 없습니다. 내 코드는 다음과 같습니다.무작위로 2 차원 배열을 중복없이 인쇄하려면 어떻게해야합니까?
static Random random = new Random(1234567);
static int i = 1;
static int a;
static int d;
static List<String> suits = new LinkedList<String>();
static List<String> cards = new LinkedList<String>();
static int[][] grid = new int[6][8];
public static void main(String[]args)
{
suits.add("Diamonds");
suits.add("Clubs");
suits.add("Hearts");
suits.add("Spades");
cards.add("1");
cards.add("2");
cards.add("3");
cards.add("4");
cards.add("5");
cards.add("6");
cards.add("7");
cards.add("8");
cards.add("9");
cards.add("10");
cards.add("11");
cards.add("12");
drawGrid();
}
private static void drawGrid()
{
for(int b = 0; b < grid.length; b++)
{
for(int c = 0; c < grid[i].length; c++)
{
a = (int)(Math.floor(suits.size() * Math.random()));
d = (int)(Math.floor(suits.size() * Math.random()));
System.out.print(" |" + cards.get(d) + " " + suits.get(a) + "|");
Collections.shuffle(suits);
Collections.shuffle(cards);
}
System.out.println();
}
}
현재 코드의 문제점은 무엇입니까? 그것은 효과가 있느냐 없느냐? – SMA
각 소송에서 13 개가 있다는 것을 알고 있습니다. 에이스 -> 10 Jack Queen King –
왜 정적 인'Random' 객체를 가지고 있고 어디에서 Math.random()을 사용합니까? –