简体   繁体   English

填充3D数组

[英]Filling a 3d array

My problem is in the last method "Printhands", I am trying to pass value from the 2d array called hands to the 3d array called fourDecks. 我的问题是在最后一个方法“ Printhands”中,我试图将值从称为“ hands”的2d数组传递给名为“ fourDecks”的3d数组。 I managed to pass the 1st dimesional array deckOfCard to the 2d array hands but the method doesn't seem to work for 3d array.And Also its a must I use 3d array no other choice...TY. 我设法将第一个2D数组deskOfCard传递给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];
                    }
                }




            }
        }
    }
}}

Error is here: 错误在这里:

fourDecks[deck][row][col] = hands[z][y];

you are trying to assign integer value to a string 您正在尝试将整数值分配给字符串

use Integer.toString() or something like that 使用Integer.toString()或类似的东西

This is error in logic 这是逻辑错误

for(int z = 0; z < hands.length; z++){

                for(int y = 0; y<hands[z].length;z++){

                    fourDecks[deck][row][col] = hands[z][y];
                }

You keep assigning hands[z][y] to the same location in fourDecks - the fourDecks indices dont advance so only the last z, y will be kept. 您一直将手[z] [y]分配给fourDecks中的相同位置-fourDecks索引不会前进,因此仅保留最后一个z,y。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM