简体   繁体   English

如何在android中创建动态变量

[英]how to create a dynamic variable in android

I am creating a card game (blackJack) in android. 我正在用android创建纸牌游戏(blackJack)。 the first two cards are easy card1 and card2, however i want to press a Hit me button and deal a new card and assign it to card3, card4 etc is there any way to do this without essentially creating the maximum number of variables you would need and using if then statements to check if they have been assigned a value or not? 前两张卡是简单的card1和card2,但是我想按一下Hit me按钮并发一张新卡并将其分配给card3,card4等,有什么方法可以做到这一点,而实际上并不需要创建所需的最大变量数并使用if then语句检查是否已为其分配值?

Use a List to keep track of your dealt cards instead of having individual variables. 使用List来跟踪已发卡,而不要使用单个变量。

private List<Card> dealtCard = new LinkedList<Card>();

you can then add and remove your cards easily,and dynamically. 然后,您可以轻松,动态地添加和删除卡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM