如何在java中打印嵌套在数组中的对象

发布于 2024-10-20 07:10:12 字数 2148 浏览 5 评论 0原文

我正在创建的纸牌游戏程序应该提示用户输入玩家数量以及每手有多少张牌,然后洗牌,然后发牌,然后显示每个玩家手中的牌。除非牌组中没有足够的牌,否则它将显示所有玩家的手牌(我们无法从一副 52 张牌中发出 10 手 7 张牌)。

我创建了一个 Card 类、一个 Deck 类、一个 Hand 类和运行该程序的驱动程序。由于这是一项作业,我必须遵守给定的规则,因此我只允许使用数组,而不是数组列表。我的问题是我无法弄清楚如何在 Hands 数组内的 Hand 对象内打印 Card 对象。可能还有更好的方法来做到这一点,但我仅限于允许导入/使用的内容。我可以获得有关在何处查找嵌套数组内的打印对象的帮助吗?由于没有对象名称,我无法使用在 Hand 类中编写的方法将卡片添加到数组中的 Hands 中。

编辑:我让 Hand 类中的构造函数创建一个大小为传递给它的 int 的手。令人困惑的是,当我从用户那里得知有多少玩家确实在玩游戏时,我在驱动程序中创建了一个 Hand 类型的数组,其中使用循环填充了新的 Hand 对象。那时我不知道如何引用每个单独的手对象,因此我可以使用 toString() 打印内容。他们似乎没有名字。

遵循的代码:

import java.util.Arrays;
import java.util.Scanner;
public class CardsDriver {

public static void main(String[] args) 
{
    Card c1 = new Card();       //create new card object
    Scanner kb = new Scanner(System.in);
    int cards;
    int players;


    System.out.print("How many players are in the game?");
    players = Integer.parseInt(kb.nextLine());
    System.out.print("\nHow many cards will be dealt to each player?");
    cards = Integer.parseInt(kb.nextLine());

    while ((cards * players) > 52)
    {
        System.out.print("There are not enough cards in the deck to deal " +
                         players + " hands of " + cards + " cards. try again.");

        System.out.print("How many players are in the game?");
        players = Integer.parseInt(kb.nextLine());
        System.out.print("\nHow many cards will be dealt to each player?");
        cards = Integer.parseInt(kb.nextLine());
    }

    Deck readyDeck = new Deck();    //create new deck 
    readyDeck.shuffleDeck();        //shuffle the newly built deck using Java.Util.Random   
    Hand[] playerHands= new Hand[players];  //create another array to hold all player hands

    for(int index =0; index < playerHands.length; index++)
    {
        playerHands[index] = new Hand(cards);   //create hand object for each player of the size input by the player
        /*for (int index2 =0; index2<cards;index2++)
        {
            //fill each hand with cards using addcard somehow. i have no object name.
            Hand.addCard(readyDeck.dealACard());
        }*/
    }
}

The card game program I am creating should prompt the user for the number of players, and how many cards in each hand, then shuffle the Deck, then deal cards, then display the cards in each players hand. It will display the Hands for all players unless there are not enough Cards in the Deck (we cannot deal 10 Hands of 7 Cards from a Deck of 52 Cards).

I have created a Card class, a Deck class, a Hand class, and driver to run the program. Since this is an assignment I must stick to the rules given, and so I am only allowed to use arrays, not array lists. My problem is that I cannot figure out how to print the Card objects inside the Hand object, inside the array of Hands. There is also probably a much better way to do this, but I am limited to what I am allowed to import / use. Can I get some help on where to look for printing objects inside nested arrays? I cannot use my method written in the Hand class to add cards to the Hands in an array, due to not having an object name.

EDIT: I have the constructor in the Hand class create a hand sized on the int passed to it. The confusion is that when I get how many players are indeed playing the game from the user, I create an array of type Hand in the driver, which is filled with new Hand objects using a loop. At that point I have no idea how to reference each individual hand object so I can print the contents using toString(). They do not appear to have a name.

Code to follow:

import java.util.Arrays;
import java.util.Scanner;
public class CardsDriver {

public static void main(String[] args) 
{
    Card c1 = new Card();       //create new card object
    Scanner kb = new Scanner(System.in);
    int cards;
    int players;


    System.out.print("How many players are in the game?");
    players = Integer.parseInt(kb.nextLine());
    System.out.print("\nHow many cards will be dealt to each player?");
    cards = Integer.parseInt(kb.nextLine());

    while ((cards * players) > 52)
    {
        System.out.print("There are not enough cards in the deck to deal " +
                         players + " hands of " + cards + " cards. try again.");

        System.out.print("How many players are in the game?");
        players = Integer.parseInt(kb.nextLine());
        System.out.print("\nHow many cards will be dealt to each player?");
        cards = Integer.parseInt(kb.nextLine());
    }

    Deck readyDeck = new Deck();    //create new deck 
    readyDeck.shuffleDeck();        //shuffle the newly built deck using Java.Util.Random   
    Hand[] playerHands= new Hand[players];  //create another array to hold all player hands

    for(int index =0; index < playerHands.length; index++)
    {
        playerHands[index] = new Hand(cards);   //create hand object for each player of the size input by the player
        /*for (int index2 =0; index2<cards;index2++)
        {
            //fill each hand with cards using addcard somehow. i have no object name.
            Hand.addCard(readyDeck.dealACard());
        }*/
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流年已逝 2024-10-27 07:10:12

假设您已经重写了每个提到的类的 toString 方法,打印数组应该像调用 Arrays.toString() 方法一样简单(而不是仅仅传递对 System.out.println() 及其变体的数组引用

如果您想知道如何处理嵌套数组,Deck 类的 toString() 方法将涉及到。在 Card 数组上调用 Arrays.toString 方法

示例:

class Card {

    int suit, rank;

    @Override
    public String toString() {
        // outputs something like ACE of SPADE
        return suitToName(suit) + " of " + rankToName(rank);
    }

}

class Deck {

    private Card[] cards;

    private String name;

    @Override
    public String toString() {
        // prints : MyDeck -> [ACE of SPADE, ACE of HEART] etc.
        return name + " -> " + Arrays.toString(cards);
    }

}

顺便说一句,我认为您应该枚举 Rank 和 Suits,而不是搞乱整数。

Assuming you have overridden the toString method of each of the mentioned classes, printing out arrays should be as simple as invoking the Arrays.toString() method (rather than just passing the array reference to System.out.println() and its variants.

In case you are wondering how this handles nested arrays, the toString() method of the Deck class would involve calling the Arrays.toString method on the Card array.

Example:

class Card {

    int suit, rank;

    @Override
    public String toString() {
        // outputs something like ACE of SPADE
        return suitToName(suit) + " of " + rankToName(rank);
    }

}

class Deck {

    private Card[] cards;

    private String name;

    @Override
    public String toString() {
        // prints : MyDeck -> [ACE of SPADE, ACE of HEART] etc.
        return name + " -> " + Arrays.toString(cards);
    }

}

BTW, I think you should enums for Rank and Suits rather than messing around with integers. That would be much cleaner I should say.

楠木可依 2024-10-27 07:10:12

正如 Sanjay 建议的那样,您应该将其添加到您的 Card 类中:

@Override 
public String toString() {
...
}

然后只要您想打印 Card 对象(无论它在哪里),就可以使用 cardinstance.toString()

我不知道你想完成什么,但这里有一个很好的 java 编程示例(用于创建德州扑克)。

http://code.google.com/p/texasholdem-java/source/checkout

看看这个,看看它是如何正确完成的,你就可以玩任何纸牌游戏了。

as Sanjay suggested you should add this to your Card class :

@Override 
public String toString() {
...
}

Then just use cardinstance.toString() whenever you want to print your Card object no matter where it is.

I don't know what are you trying to accomplish but here is a good example of good programming in java(for creating Texas Hold Em).

http://code.google.com/p/texasholdem-java/source/checkout

Checkout this see how its done properly and you're good to go with any card game.

递刀给你 2024-10-27 07:10:12

要覆盖它,您可以使用较新版本 Eclipse 中的新向导,也可以使用插件,例如 JUtils ToString Generator 我曾经写过。

To override it, you can either make use of the new wizard in the newer versions of Eclipse or use a plugin such as JUtils ToString Generator which I once wrote.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文