如果有更好的方法在 java 中编写此 if 语句

if there a better way to write this if statment in java

那么有没有一种方法可以简化它以使其更小?

else if(selectedCards.size() == 3
            && cardAt(selectedCards.get(0)).pointValue() + cardAt(selectedCards.get(1)).pointValue() + cardAt(selectedCards.get(2)).pointValue() == 0 
            && !cardAt(selectedCards.get(0)).rank().equals(cardAt(selectedCards.get(1)).rank()) 
            && !cardAt(selectedCards.get(0)).rank().equals(cardAt(selectedCards.get(2)).rank())
            && !cardAt(selectedCards.get(1)).rank().equals(cardAt(selectedCards.get(2)).rank()))

据我所知,您是在尝试测试这 3 张牌的点数是否不同。一种更简单的测试方法是将它们放入一个集合中,然后查看集合大小是否与所选集合相同。这可以扩展到任意数量的选定卡片...

public boolean differentRanks(List<Integer> selectedCards) {
    Set<Integer> ranks = new HashSet<Integer>();
    for (int card : selectedCards) {
        ranks.add(cardAt(card).rank());
    }
    return ranks.size() == selectedCards.size();
}

我还会创建一个方法来计算所选卡片的总点数...

public int sum(List<Integer> selectedCards) {
    int total;
    for (int card : selectedCards) {
        total += cardAt(card).pointValue();
    }
    return total;
}

所以条件会结束

} else if (selectedCards.size() == 3 && sum(selectedCards) == 0 && 
           differentRanks(selectedCards) {

这是一个选项:

else if(selectedCards.size() == 3
        && cardAt(selectedCards.get(0)).pointValue() +        cardAt(selectedCards.get(1)).pointValue() + cardAt(selectedCards.get(2)).pointValue() == 0 
        && !(cardAt(selectedCards.get(0)).rank().equals(cardAt(selectedCards.get(1)).rank())).equals(cardAt(selectedCards.get(2)).rank()) )

为了使这个条件更具可读性,你可以这样做:

//here you extract the values you need only once and use them in your condition block below
        int cardsSize = selectedCards.size();
        int pointValue0 = cardsSize == 3 ? cardAt(selectedCards.get(0)).pointValue() : 0;
        int pointValue1 = cardsSize == 3 ? cardAt(selectedCards.get(1)).pointValue() : 0;
        int pointValue2 = cardsSize == 3 ? cardAt(selectedCards.get(2)).pointValue() : 0;           
        bool rankEquals = CompareRanks(cardAt(selectedCards.get(0)),cardAt(selectedCards.get(1)),cardAt(selectedCards.get(2));

        if (<condition>) {
            //block of sentences
        } else if (cardsSize == 3 && (pointValue0 + pointValue1 + pointValue2) == 0 && !rankEquals )

我建议创建一个名为 "CompareRanks" 的函数,您可以在其中接收 3 个不同的对象("cardAt" 的结果)并在其中获得排名并比较值是否相同与否。

此选项会使您获得更多代码行,但对于除您之外的任何人来说都更清晰、更易读。

我认为最具可读性:

else if(selectedCards.size() == 3 && checkRanks(selectedCards))
{
    //...
}

//...

private boolean checkRanks(List<Card> cards)
{
    Card zeroCard = cardAt(selectedCards.get(0));
    Card firstCard = cardAt(selectedCards.get(1));
    Card secondCard = cardAt(selectedCards.get(2));

    boolean isZero = zeroCard.pointValue() + firstCard.pointValue() +      secondCard.pointValue() == 0;

    boolean zeroCardRankNotEqualFirst = !zeroCard.rank().equals(firstCard.rank())
    boolean zeroCardRankNotEqualSecond = !zeroCard.rank().equals(secondCard.rank())
    boolean firstCardRankNotEqualsSecond = !firstCard.rank().equals(secondCard.rank()); 

    return isZero && zeroCardRankNotEqualFirst && zeroCardRankNotEqualSecond && firstCardRankNotEqualsSecond;
}