【发布时间】:2018-01-16 12:41:05
【问题描述】:
我正在制作一款扑克游戏,我试图测试看看是否有玩家手中有皇家同花顺。然而,在我进行测试之前,我遇到了一个障碍:我没有一种快速简单的方法来测试每个玩家的 A、K、Q、J 和 10 的手牌。
到目前为止,这是我的代码:
private void findWinner() {
// p1 hand
List<String> p1hand = cards.subList(10, 15);
p1hand.addAll(cards.subList(0, 2));
System.out.println("Player 1's total hand is " + p1hand);
// p1 hand
List<String> p2hand = cards.subList(10, 15);
p2hand.addAll(cards.subList(2, 4));
System.out.println("Player 2's total hand is " + p2hand);
// testing for royal flush
String[] spadeRoyalFlush = {"Ace of spades", "King of spades", "Queen of spades", "Jack of spades", "10 of spades"};
String[] heartRoyalFlush = {"Ace of hearts", "King of hearts", "Queen of hearts", "Jack of hearts", "10 of hearts"};
String[] dimRoyalFlush = {"Ace of diamonds", "King of diamonds", "Queen of diamonds", "Jack of diamonds", "10 of diamonds"};
String[] clubRoyalFlush = {"Ace of clubs", "King of clubs", "Queen of clubs", "Jack of clubs", "10 of clubs"};
// p1
boolean p1royalFlush = false;
if(p1hand.contains(spadeRoyalFlush)) { // line 104
p1royalFlush = true;
}
if(p1hand.contains(heartRoyalFlush)) {
p1royalFlush = true;
}
if(p1hand.contains(dimRoyalFlush)) {
p1royalFlush = true;
}
if(p1hand.contains(clubRoyalFlush)) {
p1royalFlush = true;
}
if(p1royalFlush) {
System.out.println("Player 1 got a royal flush with " + p1hand);
}
// p2
boolean p2royalFlush = false;
for(int i=0; i<p2hand.size(); i++) {
if(p2hand.contains(spadeRoyalFlush)) {
p2royalFlush = true;
}
if(p2hand.contains(heartRoyalFlush)) {
p2royalFlush = true;
}
if(p2hand.contains(dimRoyalFlush)) {
p2royalFlush = true;
}
if(p2hand.contains(clubRoyalFlush)) {
p2royalFlush = true;
}
}
if(p2royalFlush) {
System.out.println("Player 2 got a royal flush with " + p2hand);
}
} // line 165 is where I call this method
cards 是一个字符串数组列表,其中包含每张牌的每个名称(例如“黑桃 A”、“红桃 3”等)
当我运行这段代码时,我得到了错误:
Exception in thread "main" java.util.ConcurrentModificationException
at java.base/java.util.ArrayList$SubList.checkForComodification(Unknown Source)
at java.base/java.util.ArrayList$SubList.listIterator(Unknown Source)
at java.base/java.util.AbstractList.listIterator(Unknown Source)
at java.base/java.util.ArrayList$SubList.iterator(Unknown Source)
at java.base/java.util.AbstractCollection.contains(Unknown Source)
at poker.PlayGame.findWinner(PlayGame.java:104)
at poker.PlayGame.doEverything(PlayGame.java:165)
at poker.MainPoker.main(MainPoker.java:7)
我研究了这意味着什么,我感到很震惊,因为据我所知,我没有对代码中的任何内容进行迭代。
这是 DoEverything 方法:
public void doEverything() {
dealHands();
throwCards();
findWinner(); // line 165
}
这是主要的方法:
public static void main(String[] args) {
PlayGame game = new PlayGame();
game.doEverything(); // line 7
}
【问题讨论】:
-
遍历数组并调用 list.contains(currentIterationElement);现在,如果其中之一在列表中,则将其设置为 true。或者,您可以在 if 语句中使用 && 运算符(逻辑 AND 运算符)
-
您可能需要改写标题,因为它几乎不能反映您的问题。请显示其余代码并标记/突出显示错误消息中指定的行(第 7、104 和 165 行)。
-
@Turing85 我标记为 104,165 是我在其中调用此方法的公共 void,而 7 是我在 165 处调用该方法的位置,是我的主要方法。
-
@TheMagician Again:请发布代码。没有看到相关部分,(即至少方法
main(...)和doEverything(...),我们无法判断为什么会发生此异常。 -
@TheMagician 请查看链接的副本。 TL;DR:你得到
Exception,因为你在结构上修改了subList。 documentation ofList::subList(int, int)声明:“返回的列表受此列表的支持,因此返回列表中的非结构性更改会反映在此列表中,反之亦然。”
标签: java arrays string arraylist