gpt4 book ai didi

java - 使用方法和多个类的一副纸牌

转载 作者:行者123 更新时间:2023-11-29 04:48:07 24 4
gpt4 key购买 nike

我没有得到任何输出,也不确定从这里去哪里。

Design and implement a class called Card, which represents a standard playing card. Each card has a suit and a face value. Then, create a driver class that stores 52 objects of the Card class into an array. Include methods to shuffle the deck, deal a card and report the number of cards left in the deck. The shuffle method should assume a full deck. Your main method should deal each card from a shuffled deck, printing each card (suit and face value) as it is dealt.

这是我目前所拥有的:

    import java.util.Random;
public class card {
public static void main(String[] args) {}

public class deck {


int[] deck = new int[52];
String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

public void create() {//initialize cards
for (int i = 0; i < deck.length; i++) deck[i] = i;
}


public void shuffle() {//deck shuffle
for (int i = 0; i < deck.length; i++) {

int index = (int) (Math.random() * deck.length);
int temp = deck[i];
deck[i] = deck[index];
deck[index] = temp;
}



//display all the cards!
for (int i = 0; i < deck.length; i++)

{
String suit = suits[deck[i] / 13];
String rank = ranks[deck[i] % 13];
System.out.println("Card number " + deck[i] + ": " + rank + " of " + suit);


}


}
}

}

最佳答案

在 Java 中,方法 main() 是代码开始运行的地方。您现在的 main 方法中没有任何内容:

public static void main(String[] args) {}

要让它做某事,将其更改为如下所示:

public static void main(String[] args) {
deck myDeck = new deck();
myDeck.create();
myDeck.shuffle();
}

请注意,在 Java 中使用大写字母命名类是一种很好的做法。

关于java - 使用方法和多个类的一副纸牌,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36390659/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com