无法在 main 方法中访问数组

Can't access array in the main method

我正在尝试创建一个 class,在一种方法中,它创建一副纸牌,在另一种方法中,我希望它能将纸牌洗牌 1000 次,然后然后打印到控制台。 (这是我老师告诉我做的实验) 我的问题是我在 CardDeck 方法中创建了数组,我无法在该方法之外访问它们。我正在寻找一个对于编程新手来说很简单的答案,可以理解并将其应用到我当前的代码中。 这个程序写在java.

import java.util.Random;

public class Deck {
    public static void main(String args[]) {
        // mah lovely arrays ^.^
        CardDeck();

        Random random = new Random();
        for (int q = 0; q < 52; q++) {
            int rand = random.nextInt(52);
        }
    }

    public static void CardDeck() {

        final String[] deckSuit = new String[52];
        String[] deckKind = new String[52];
        int[] deckValue = new int[52];

        String spade = "Spades";
        String diamond = "Diamonds";
        String heart = "Hearts";
        String club = "Clubs";

        // set the respective suits
        for (int q = 0; q < 13; q++) {
            deckSuit[q] = spade;
        }
        for (int q = 13; q < 26; q++) {
            deckSuit[q] = diamond;
        }
        for (int q = 26; q < 39; q++) {
            deckSuit[q] = heart;
        }
        for (int q = 39; q < 52; q++) {
            deckSuit[q] = club;
        }
        // set the kind of card
        for (int q = 0; q < 52; q += 13) {
            deckKind[q] = "Two";
            deckKind[q + 1] = "Three";
            deckKind[q + 2] = "Four";
            deckKind[q + 3] = "Five";
            deckKind[q + 4] = "Six";
            deckKind[q + 5] = "Seven";
            deckKind[q + 6] = "Eight";
            deckKind[q + 7] = "Nine";
            deckKind[q + 8] = "Ten";
            deckKind[q + 9] = "Jack";
            deckKind[q + 10] = "Queen";
            deckKind[q + 11] = "King";
            deckKind[q + 12] = "Ace";
        }

        for (int q = 0; q < 52; q += 13) {
            deckValue[q] = 2;
            deckValue[q + 1] = 3;
            deckValue[q + 2] = 4;
            deckValue[q + 3] = 5;
            deckValue[q + 4] = 6;
            deckValue[q + 5] = 7;
            deckValue[q + 6] = 8;
            deckValue[q + 7] = 9;
            deckValue[q + 8] = 10;
            deckValue[q + 9] = 10;
            deckValue[q + 10] = 10;
            deckValue[q + 11] = 10;
            deckValue[q + 12] = 11;
        }


        //display all the cards
        for (int q = 0; q < 52; q++) {
            System.out.println("[" + deckSuit[q] + ", " + deckKind[q] + ", " + deckValue[q] + "]");
        }
    }
    //declare ye methods here
}

您的括号格式不正确。你在 `public static void CardDeck().

上面多了一个大括号

固定代码:

import java.util.Random;


public class Deck {

    public static void main(String args []){
        // mah lovely arrays ^.^


        CardDeck();

        Random random = new Random();
        for(int q = 0;q<52;q++){
        int rand = random.nextInt(52);

        }
    }

    public static void CardDeck(){

           final String[] deckSuit = new String[52];
            String[] deckKind = new String[52];
               int[] deckValue = new int[52];

            String spade = "Spades";
            String diamond = "Diamonds";
            String heart = "Hearts";
            String club = "Clubs";



            // set the respective suits
            for(int q = 0;q<13;q++){
                deckSuit[q] = spade;
            }
            for(int q = 13;q<26;q++){
                deckSuit[q] = diamond;
            }
            for(int q = 26;q<39;q++){
                deckSuit[q] = heart;
            }
            for(int q = 39;q<52;q++){   
                deckSuit[q] = club;
            }
            // set the kind of card
            for(int q = 0;q<52;q+=13){
                deckKind[q] = "Two";
                deckKind[q+1] = "Three";
                deckKind[q+2] = "Four";
                deckKind[q+3] = "Five";
                deckKind[q+4] = "Six";
                deckKind[q+5] = "Seven";
                deckKind[q+6] = "Eight";
                deckKind[q+7] = "Nine";
                deckKind[q+8] = "Ten";
                deckKind[q+9] = "Jack";
                deckKind[q+10] = "Queen";
                deckKind[q+11] = "King";
                deckKind[q+12] = "Ace";
            }

            for(int q = 0;q<52;q+=13){
                deckValue[q] = 2;
                deckValue[q+1] = 3;
                deckValue[q+2] = 4;
                deckValue[q+3] = 5;
                deckValue[q+4] = 6;
                deckValue[q+5] = 7;
                deckValue[q+6] = 8;
                deckValue[q+7] = 9;
                deckValue[q+8] = 10;
                deckValue[q+9] = 10;
                deckValue[q+10] = 10;
                deckValue[q+11] = 10;
                deckValue[q+12] = 11;
            }


            //display all the cards
            for(int q = 0;q<52;q++){

                System.out.println("["+deckSuit[q]+", "+deckKind[q]+", "+deckValue[q]+"]");
                }   

        //declare ye methods here
        }

}

几个解决方案

  1. 使数组静态化public

  2. return 来自函数的数组

因为您在 CardDeck 方法中创建/声明了数组,所以您不能在该方法之外使用。

您可以将数组声明为实例变量并在 CardDeck() 方法中初始化。

第一个解决方案是在更高的范围内声明您的变量。您可以在 Deck class 中创建一个可通过任何静态方法访问的静态成员(在本例中为 main()CardDeck()),或者您可以在 [=10] 中创建一个变量=] 方法并将其作为 CardDeck(String[]) 方法的参数传递。

但我认为更好的解决方案是将 CardDeck() 方法的原型更改为 String[] createDeck()。该方法首先创建甲板,然后 returns 它。在 main() 方法中,然后将返回值存储在可在 for 循环中使用的本地 String[] 中。