我无法在此代码中获取某张卡片的属性,但始终会打印null
并且我不知道为什么。我来自Php-HTML世界,我是JAVA的新手。
这是来自Opportunity.java
package opportunity;
import java.io.*;
import java.util.Collections;
import java.util.List;
import static java.util.stream.Collectors.toList;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import opportunity.Card.CardType;
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;
/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException {
System.out.print("***************Opportunity***************\n");
final Card card1 = new Card(Card.CardType.EVENT, "Get Tax Returns").
setProperty("cost", "0.00").
setProperty("Effect", "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").
setProperty("cost", "0.00").
setProperty("Effect", "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").
setProperty("cost", "10000.00").
setProperty("Effect", "Effect: The total income of all\n"
+ "the players becomes equal to\n"
+ "the income of the player\n"
+ "with the lowest income.");
final List<Card> deck = Stream.of(CardType.values()).
flatMap(type -> IntStream.rangeClosed(1, 4).mapToObj(num -> new Card(type, "CardName" + num))).
collect(toList());
Collections.shuffle(deck);
System.out.println(deck.get(0).getProperty("Effect"));
}
}
这是来自Card.java
package opportunity;
import java.util.*;
public class Card {
public enum CardType {
EVENT,
PROPERTY,
ASSET;
}
private final CardType cardType;
private final String cardName;
private final Map<String, String> properties = new HashMap<>();
Card(final CardType cardType, final String cardName) {
this.cardType = cardType;
this.cardName = cardName;
}
public Card setProperty(final String name, final String value) {
properties.put(name, value);
return this;
}
public String getProperty(final String name) {
return properties.get(name);
}
}
它总是会返回:
run:
***************Opportunity***************
null
BUILD SUCCESSFUL (total time: 0 seconds)
即使我在get()
答案 0 :(得分:1)
要建立@Luiggi门多萨的答案,您不会将您的卡片对象添加到deck
列表。
替换
final List<Card> deck = Stream.of(CardType.values()).
flatMap(type -> IntStream.rangeClosed(1, 4).mapToObj(num -> new Card(type, "CardName" + num))).
collect(toList());
用
final List<Card> deck = new ArrayList<>();
deck.add(card1);
deck.add(card2);
deck.add(card3);
不是在列表中添加新的Cards
,而是添加已创建的对象,并设置属性。
编辑:解决您的评论
final List<Card> deck = new ArrayList<>();
for (int i=0; i<4; i++) {
deck.add(card1);
deck.add(card2);
deck.add(card3);
}
这会将三张卡中的每张卡的四份副本添加到列表中。
答案 1 :(得分:0)
下面:
final List<Card> deck = Stream.of(CardType.values()).
flatMap(type -> IntStream.rangeClosed(1, 4).mapToObj(num -> new Card(type, "CardName" + num))).
collect(toList());
Collections.shuffle(deck);
您使用新的List<Card> deck
填充Card
,不添加任何其他cardX
变量。因此,在检索此"Effect"
属性时,您会得到null
,因为Card
中的deck
之前没有设置"Effect"
属性。
答案 2 :(得分:0)
前人的评论是完全正确的。您没有将创建的卡添加到列表中。
尝试替换您的代码
final List<Card> deck = Stream.of(CardType.values()).
flatMap(type -> IntStream.rangeClosed(1, 4).mapToObj(num -> new Card(type, "CardName" + num))).
collect(toList());
用于标准列表创建和添加。例如(并更新为4张卡):
List<Card> deck = new ArrayList<Card>();
for (int i = 0; i < 4; i++){
deck.add(card1);
deck.add(card2);
deck.add(card3);
}
如果要最小化代码行数,可以使用内容初始化新列表。试试这篇文章Initialization of an ArrayList in one line。
无论如何,总是尝试编写最简单的解决方案,在使用新语言时更是如此。