所以我是编程初学者,我在课堂上有一个项目,要求我打一手桥牌并将其分发给四名玩家。其中一项任务要求我打印出一副洗牌的牌,但是,当我尝试打印它时,它输出 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 回答
catspeake
TA贡献1111条经验 获得超0个赞
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将数组声明为局部变量并将其作为参数传递给这些方法。
添加回答
举报
0/150
提交
取消