gpt4 book ai didi

java - 避免类在 ArrayList 上添加新对象

转载 作者:行者123 更新时间:2023-12-02 05:31:32 24 4
gpt4 key购买 nike

我已经为一个简单的纸牌游戏编写了代码。每次我抽牌都不会减少,而且我不知道如何避免。牌堆上有 24 张牌。每次我回答 Y 时,它都会从牌堆中抽一张牌并将其删除,但它不会在我的代码中发生。

这是我的代码:

Opportunity.java

package opportunity;
import java.io.*;

public class Opportunity {
/*Player 1 */
public int p1_money = 10000;
public int p1_card_d = 40;
public int p1_card_h = 0;
/*Player 2 */
public int p2_money = 10000;
public int p2_card_d = 40;
public int p2_card_h = 0;
public int xyz = 0;

/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException {
System.out.print("***************Opportunity***************\n");
while (true) {
System.out.println("Do you want to draw a card? Y/N");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
if ("Y".equals(s)) {
Deck deck = new Deck();
if (deck.card_deck.get(0).getEnum() == Card.CardType.EVENT) {
System.out.println("Name: "+deck.card_deck.get(0).getName());
System.out.println("Cost: "+deck.card_deck.get(0).getCost());
System.out.println("Effect: "+deck.card_deck.get(0).getProperty("Effect"));
deck.card_deck.remove(0);
System.out.println(deck.card_deck.size());
}
else if(deck.card_deck.get(0).getEnum() == Card.CardType.ASSET) {
System.out.println("Name: "+deck.card_deck.get(0).getName());
System.out.println("Purchase Cost: "+deck.card_deck.get(0).getCost());
System.out.println("Income: "+deck.card_deck.get(0).getProperty("income"));
System.out.println("Start Selling Price: "+deck.card_deck.get(0).getProperty("ssp"));
System.out.println("Selling Price Growth: "+deck.card_deck.get(0).getProperty("spg")+" per turn");
deck.card_deck.remove(0);
System.out.println(deck.card_deck.size());
}
} else {
System.out.println("Goodbye!");
break;
}
}
}
}

Card.java:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package opportunity;

import java.util.*;


/**
*
* @author ASUS
*/
public class Card {
public enum CardType {
EVENT,
PROPERTY,
ASSET;
}
private final CardType cardType;
private final String cardName;
private final Double cardCost;
private final Map<String, String> properties = new HashMap<>();

Card(final CardType cardType, final String cardName, final Double cardCost) {
this.cardType = cardType;
this.cardName = cardName;
this.cardCost = cardCost;
}

public Card setProperty(final String name, final String value) {
properties.put(name, value);
return this;
}
public String getName() {
return cardName;
}
public Double getCost() {
return cardCost;
}
public Enum getEnum() {
return cardType;
}
public String getProperty(final String name) {
return properties.get(name);
}
}

Deck.java

package opportunity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class Deck {
public List<Card> card_deck;
private int x;

public Deck() {
if(x == 0) {
final Card card1 = new Card(Card.CardType.EVENT, "Get Tax Returns",0.00).
setProperty("Effect", "Earn money equal to the\n"
+ "maximum income each of your\n"
+ "properties can give you,\n"
+ "depending on their level.");
final Card card2 = new Card(Card.CardType.EVENT, "BIR Hunting Begins",0.00).
setProperty("cost", "0.00").
setProperty("Effect", "An opponent loses\n"
+ "money equal to 50% of the\n"
+ "maximum income each of\n"
+ "their properties can give him or her,\n"
+ "depending on the level of the\n"
+ "property.");
final Card card3 = new Card(Card.CardType.EVENT, "Restore Balance",10000.00).
setProperty("Effect", "The total income of all\n"
+ "the players becomes equal to\n"
+ "the income of the player\n"
+ "with the lowest income.");
final Card card4 = new Card(Card.CardType.ASSET, "Vacant Property Lot",7500.00).
setProperty("income", "1000.00").
setProperty("ssp", "3250.00").
setProperty("spg", "5000.00");
final Card card5 = new Card(Card.CardType.ASSET, "Stock Investment",9000.00).
setProperty("income", "500.00").
setProperty("ssp", "4500.00").
setProperty("spg", "10000.00");
final Card card6 = new Card(Card.CardType.ASSET, "Time Investment Account",15000.00).
setProperty("income", "100.00").
setProperty("ssp", "7500.00").
setProperty("spg", "15000.00");

card_deck = new ArrayList<>();
for (int i=0; i<4; i++) {
card_deck.add(card1);
card_deck.add(card2);
card_deck.add(card3);
card_deck.add(card4);
card_deck.add(card5);
card_deck.add(card6);
}
Collections.shuffle(card_deck);
}
}
public List getList(){
x = 1;
return card_deck;
}
}

最佳答案

您在每次迭代中创建一个新的牌组:

        if ("Y".equals(s)) {
Deck deck = new Deck();

这看起来不对,因为它会使每次迭代中的 Deck 都变满。

关于java - 避免类在 ArrayList 上添加新对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25476133/

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