简体   繁体   中英

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. 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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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