我正在写一个程序,做一个卡技巧,但首先我必须弄清楚如何处理3行7张卡。我无法弄清楚为什么当我运行我的程序时,它只输出所有卡的一个数字,以及如何让它打印出卡的名称。我可以用PrintCard()函数打印52张独特的卡片,但在使用Deal()时不能打印,我不知道为什么会有任何帮助。谢谢。Java卡处理程序
这是目前我的代码:
import java.util.Random;
public class testing {
public static void main(String[] args) {
testing cs = new testing();
int [] deck = new int [52];
int [] [] play = new int [7] [3];
cs.BuildDeck (deck);
cs.Deal(deck, play);
}
public static void BuildDeck(int deck[]) {
int[] used = new int[52];
int card = 0;
int i = 0;
/*
* Generate cards until the deck is full of integers
*/
while (i < deck.length) {
/*
* generate a random number between 0 and 51
*/
Random random1 = new Random();
card = (random1.nextInt(52) % 52);
/*
* Check the used array at the position of the card. If 0, add the card and set the used location
* to 1. If 1, generate another number
*/
if (used[card] == 0) {
used[card] = 1;
deck[i] = card;
i++;
}
}
}
public static void PrintCard(int card) {
int rank = (card % 13);
int suit = (card/13);
String[] suits = {"Clubs", "Hearts", "Diamonds", "Spades"};
String[] ranks = {"King", "Ace", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "Jack", "Queen"};
System.out.println(ranks[rank] + " of " + suits[suit]);
}
public static void Deal(int deck[], int play[][]) {
int card = 0;
System.out.println ("\n Column 0 Column 1 Column 2");
System.out.println ("=======================================================\n");
for (int row = 0; row < play.length; row++) {
for (int col = 0; col < play [row].length; col++) {
play[row][col] = deck[card++];
System.out.print(" " + play[row][col] + " ");
}
System.out.println();
}
}
}
更大的计划:http://pastebin.com/99tLpVjM
雷内说什么。但是:你的建造方式+洗牌是不理想的,因为它不能保证完成。 'Collections.shuffle(Arrays.asList(array));'的行会是更好的解决方案。 – tim 2012-03-28 11:16:30
代码批判:从不使用主列中的'i'。 while循环中的'random1'只能初始化一次。卡应该在while循环中声明,因为它不在外部使用。 Deal中的'row,col,card'应该在for循环中声明。 – 2012-03-28 11:18:29
方法以大写字母开头! – mishadoff 2012-03-28 11:26:33