我的问题是在最后一个方法“Printhands”中,我试图将名为hands的2d数组中的值传递给名为fourDecks的3d数组。我设法将第一个二维阵列deckOfCard传递给2D阵列,但该方法似乎不适用于3d阵列。而且它必须使用3d阵列,没有其他选择... TY。
import java.util.Random;
public class Decks {
public static void main(String[] args) {
int[] deckOfCard = new int[52];
int[][] hands = new int[4][13];
int [][][]fourDecks = new int[4][4][13];
generateCard(deckOfCard);
printCard(deckOfCard, 13, 5);
deal(deckOfCard, hands);
}
public static void generateCard(int deckOfCard[]) {
boolean[] check = new boolean[52];
int all = 0;
int ranNum;
while (all < 52) {
ranNum = (int) (Math.random() * 52);
if (!check[ranNum]) {
check[ranNum] = true;
deckOfCard[all] = ranNum;
all++;
}
}
}
public static void printCard(int deckOfCard[], int row, int space) {
System.out.println("Print Card:");
int num = 0;
for (int print = 0; print < deckOfCard.length; print++) {
String format = "%" + space + "s";
System.out.printf(format, deckOfCard[print]);
num++;
if (num == row) {
System.out.println();
num = 0;
}
}
}
public static void deal(int deckOfCard[], int hands[][]) {
System.out.println("DEAL:");
int x = 0;
for (int row = 0; row < hands.length; row++) {
for (int col = 0; col < hands[row].length; col++) {
if (x < deckOfCard.length) {
hands[row][col] = deckOfCard[x];
x++;
}
}
}
for (int i = 0; i < hands.length; i++) {
for (int j = 0; j < hands[i].length; j++) {
System.out.print(hands[i][j] + " ");
}
System.out.println();
}
}
public static void PrintHands(String fourDecks[][][], int hands[][] ) {
String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "Jack", "Queen", "King"};
for (int deck = 0; deck < fourDecks.length; deck++) {
for (int row = 0; row < fourDecks[deck].length; row++) {
for (int col = 0; col < fourDecks[deck][row].length; col++) {
for(int z = 0; z < hands.length; z++){
for(int y = 0; y<hands[z].length;z++){
fourDecks[deck][row][col] = hands[z][y];
}
}
}
}
}
}}
答案 0 :(得分:0)
错误在这里:
fourDecks[deck][row][col] = hands[z][y];
您正在尝试将整数值分配给字符串
使用Integer.toString()或类似的东西
答案 1 :(得分:0)
这是逻辑错误
for(int z = 0; z < hands.length; z++){
for(int y = 0; y<hands[z].length;z++){
fourDecks[deck][row][col] = hands[z][y];
}
你继续将手[z] [y]分配到fourDecks中的相同位置 - fourDecks指数不会前进,所以只保留最后一个z,y。