输出显示为空

时间:2019-09-20 12:28:12

标签: java

因此,我是编程的初学者,并且我在课堂上有一个项目,要求我做一个桥梁手,将他们分配给四个玩家。其中一项任务要求我打印出经过改组的平台,但是,当我尝试将其打印时,它会输出52个空语句而不是经过改组的平台。有人可以帮我解决这个问题吗?谢谢。

import java.util.*;

public class Card {

   public static String[] SUITS = {"Clubs","Diamonds","Hearts","Spades"};
   public static String[] RANKS = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

   public static String[]deck = new String[52];

   public static String[] createDeck() { // initializes and creates deck

      for(int i = 0; i < RANKS.length ; i++) {
         for(int j = 0; j < SUITS.length ; j++) {

            deck[SUITS.length * i + j] = RANKS[i] + " of " + SUITS[j];
         }
      }
      return deck; 
   }

   public static void shuffleDeck(String[]deck) { //shuffles deck created from array
      int n = deck.length;

      for (int i = 0; i < n; i++) {

         int r = i + (int)(Math.random() * (n-i));

         String temp = deck[r];
         deck[r] = deck[i]; 
         deck[i] = temp;

         System.out.println(deck[i]);
      }  
   }   

   public static void main(String[] args) {
      Card.shuffleDeck(deck);

   }

1 个答案:

答案 0 :(得分:1)

您在createDeck()方法中缺少一个main()调用:

public static void main(String[] args) {
    createDeck();
    shuffleDeck(deck);
}

因为以下语句仅使用deck 52值初始化null数组:

public static String[]deck = new String[52];

您需要用实际值填充deck数组以正确显示它们。而且您可以安全地将createDeck()标记为void,因为您无需返回deck数组,因为它被声明为static,但仍然可以访问。与shuffleDeck()方法一样,您不需要传递static数组,它将可以访问。

或者您可以采用另一种方法,其中将deck数组声明为局部变量,并将其作为参数传递给这些方法。