다음은 코드입니다.코드 설명
기본적으로이 코드는 suit + rank가있는 52 개의 카드를 인쇄합니다.
package javaapplication52;
public class JavaApplication52 {
public static void deck() {
String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" };
String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack",
"Queen", "King", "Ace" };
// avoid hardwired constants
int SUITS = suit.length;
int RANKS = rank.length;
int N = SUITS * RANKS;
// initialize deck
String[] deck = new String[N];
for (int i = 0; i < RANKS; i++) {
for (int j = 0; j < SUITS; j++) {
deck[SUITS * i + j] = rank[i] + " of " + suit[j];
}
}
// shuffle
for (int i = 0; i < N; i++) {
int r = i + (int) (Math.random() * (N - i));
String t = deck[r];
deck[r] = deck[i];
deck[i] = t;
}
// print shuffled deck
for (int i = 0; i < N; i++) {
System.out.println(deck[i]);
}
}
public static void main(String[] arg) {
JavaApplication52 d = new JavaApplication52();
d.deck();
}
}
나 내가했다 스탠드에서하지 않았다 무엇
deck[SUITS * i + j] = rank[i] + " of " + suit[j]
에 아무 의미하지 코드는, 왜 그들이 정장 그때 J를 추가 곱 곱 않았다. 혼란스러운 것은 SUITS (4와 같음), i (13)를 곱한 다음 j (4의 값을 가짐)를 더하고 52와 전혀 같지 않음. 이 함수가 어떻게 작동했는지 설명하십시오. 인덱스가 4이기.
Maxmium 값으로부터 시작
CURRENT_COLUMN
와 indices은'j' 3. SO 4 * 12 + 3 = 51 (52 배열의 값)는 12이다. – Mics
나는 C#에서 비슷한 코드를 썼다. 혼란스러운 라인 뒤의 내부 for 루프에이 라인을 추가하면, 물건 자체를 설명 할 수있게 만든다. Console.WriteLine ("deck [{0}] = {1}", SUITS * i + j, 갑판 [SUITS * i + j]); (콘솔 (또는 명령 프롬프트)에 대한 Java 기반 출력으로 변환하십시오 ... 그리고 이해하려고 ... –
당신은 http://codereview.stackexchange.com/ – zEro