如何在java中打印嵌套在数组中的对象
我正在创建的纸牌游戏程序应该提示用户输入玩家数量以及每手有多少张牌,然后洗牌,然后发牌,然后显示每个玩家手中的牌。除非牌组中没有足够的牌,否则它将显示所有玩家的手牌(我们无法从一副 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您已经重写了每个提到的类的
toString
方法,打印数组应该像调用Arrays.toString()
方法一样简单(而不是仅仅传递对 System.out.println() 及其变体的数组引用如果您想知道如何处理嵌套数组,Deck 类的 toString() 方法将涉及到。在 Card 数组上调用 Arrays.toString 方法
示例:
顺便说一句,我认为您应该枚举 Rank 和 Suits,而不是搞乱整数。
Assuming you have overridden the
toString
method of each of the mentioned classes, printing out arrays should be as simple as invoking theArrays.toString()
method (rather than just passing the array reference toSystem.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 theArrays.toString
method on the Card array.Example:
BTW, I think you should enums for Rank and Suits rather than messing around with integers. That would be much cleaner I should say.
正如 Sanjay 建议的那样,您应该将其添加到您的 Card 类中:
然后只要您想打印 Card 对象(无论它在哪里),就可以使用
cardinstance.toString()
。我不知道你想完成什么,但这里有一个很好的 java 编程示例(用于创建德州扑克)。
http://code.google.com/p/texasholdem-java/source/checkout
看看这个,看看它是如何正确完成的,你就可以玩任何纸牌游戏了。
as Sanjay suggested you should add this to your Card class :
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.
要覆盖它,您可以使用较新版本 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.