Java 涉及2D数组的赋值有问题

Java 涉及2D数组的赋值有问题,java,arrays,Java,Arrays,我正在做一项涉及数组的作业。今天就要交了,我的教授还没有给我回复,因为现在是复活节,所以我非常感谢你的帮助 我遇到的麻烦是2D阵列和打印。我已经检查了关于堆栈溢出的其他类似问题,并尝试将它们应用到我自己的问题中,但我仍然没有完全理解 我需要做的是将数组hands打印到控制台中。我在我的交易卡for loop之外使用了for循环和增强的for循环,试图让它工作,但没有效果 package pokerapp; import java.util.Arrays; import java.util.Sc

我正在做一项涉及数组的作业。今天就要交了,我的教授还没有给我回复,因为现在是复活节,所以我非常感谢你的帮助

我遇到的麻烦是2D阵列和打印。我已经检查了关于堆栈溢出的其他类似问题,并尝试将它们应用到我自己的问题中,但我仍然没有完全理解

我需要做的是将数组hands打印到控制台中。我在我的交易卡for loop之外使用了for循环和增强的for循环,试图让它工作,但没有效果

package pokerapp;

import java.util.Arrays;
import java.util.Scanner;

public class PokerApp {

    public static void main(String[] args) {
        // TODO code application logic here
        System.out.println("Welcome to the Poker App");
        System.out.println();

        //scanner
        Scanner sc = new Scanner(System.in);
        String choice = "y";

        while (choice.equalsIgnoreCase("y")) {
        //create a deck
        Deck myDeck = new Deck();

        //shuffle cards
        myDeck.shuffleCards();
        String[][] hands = new String[4][5];

        //deal cards
        int i = 0;
        for(int k=0;k<5;k++){
            for(int j=0;j<4;j++){
                hands[j][i] = myDeck.dealCard(i);
            }
            System.out.println("Hand " + hands + ": ");
        }

        //print 5 hands with one hand on each line
        //four cards per hand
        /* output should look something like this:
        Hand 1: H4, C5, S7, H5
        Hand 2: H3, C3, S10, S11
        ...
        */





        //ask if user wants to continue
        System.out.println("Would you like to continue? Y/N?");
        choice = sc.nextLine();
        System.out.println();
       }
    } 
}
下面是我的甲板课

public class Deck {
    private String[] suites = {"H","D","C","S"};
    private String[] cards = new String[52];

    public Deck() {
        //create deck of cards
        int index = 0;
        //loop through the suite
        for(String suite: suites){
            //generate the ace through king
            for(int j = 1; j<14;j++){
                cards[index] = suite + j;
                index++;
            } //end of ace through king
        }//end of suites
    }//end of constructor

    public void printCardArray(){
        int index = 0;

        for(String suite : suites){
            for(int j =0; j<13;j++){
                System.out.print(cards[index] + " ");
                index++;
            }
        }
    }//end of print card array

    public void shuffleCards(){
        //execute random number 100 times
        for(int i=0;i<100;i++){
            String savedCard = "";
            int variant = ((int)(Math.random()*50)) + 1;
            for (int j = 0; j <  cards.length; j++){
               if (j + variant < cards.length){
                   savedCard = cards[j];
                   cards[j] = cards[j + variant];
                   cards[j + variant] = savedCard;
               } 
            }
        }
    }//end of shuffleCards

    public String dealCard(int index){
        return cards[index];
    } 
}
我需要做的是将数组hands打印到控制台中

打印数组的方法有很多种,这取决于您希望输出的显示方式

您可以这样打印阵列:

for (String[] array : hands) {
     System.out.println(Arrays.toString(array));
}
或者这样:

for (String[] array : hands) {
   for (String str : array) {
       System.out.println(str);
   }
}
另一种方式:

System.out.println(Arrays.deepToString(hands));

从原始代码中提取

for(int k=0;k<5;k++){

    System.out.print("Hand " + k + ": ");

    for(int j=0;j<4;j++){
        hands[j][k] = myDeck.dealCard(k);
        System.out.print(hands[j][k]);

        if(j != 3) {
            System.out.print(", ");
        }
    }

    System.out.println();
}

用这个。但只要hands[j][k]=myDeck.dealCardk;返回H4、C5、S7、H5

我建议您查看数组。deepToString

您是否尝试过两个嵌套for循环?可能我有点困惑,但如果有5只手,每只手有4张牌,这不应该是:String[][]hands=新String[4][5];而是字符串[][]手=新字符串[5][4];这是我最初使用的,但我不确定如何让它以我想要的方式输出我的信息。我需要将我的输出与我在评论中作为示例显示的一样,但是其他堆栈溢出问题中的建议方法不起作用。Cody,缺少什么?手1:,手2:零件?如果是这样,只需引入一个计数器int i,并在println.Cody的开头添加Hand+Integer.toStringi++,然后每个的嵌套就是您要寻找的,只需按照@domdom的建议创建一个计数器即可。@codymars如果您需要整理程序,您需要的是打印数组方面的帮助,而不是调试整个程序。我建议你再检查一遍你的赋值,看看它为什么显示空值。是的,我现在对数组的问题似乎比我的输出更严重。我已经实现了这些循环,它们都工作了,它们在我之前输入时没有,一定是我的代码中的一个错误。我的输出太可怕了,一定是把我的交易卡搞砸了。谢谢大家的帮助,非常感谢!一切都解决了!我相信我遇到的问题是使我的打印循环与交易卡循环分离。非常感谢你不客气我的朋友,你能接受这个答案吗?
    Hand 1: H4, C5, S7, H5
    Hand 2: H3, C3, S10, S11