简体   繁体   English

增强型 For 循环卡住

[英]Enhanced For-Loop Gets Stuck

I have two files PokerCards and Frame.我有两个文件 PokerCards 和 Frame。

PokerCards is enumeration *my suits work fine*
enum Number {
        Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King;

        public static final Number[] numbers = Number.values();
        public static Number getNumber(int n){
            return Number.numbers[n];
        }
    }

    private final Number number;

    //constructor
    public PokerCards(final Suit suit, final Number number){
        this.suit = suit;
        this.number = number;
    }


    public Number getNumber(){
        return this.number;
    }

Below is Frame which is to be my GUI.下面是我的 GUI 框架。 My error checking for suits work perfectly but my numbers...not so much.我对西装的错误检查非常有效,但我的数字……没那么多。 I am trying to use an enhanced for-loop to match the corresponding enumeration我正在尝试使用增强的 for 循环来匹配相应的枚举

//card1Num is the value of the card such as Ace, Three, King and is recorded via a TextField via the GUI.

card1Num = card1TextFieldNum.getText();
if (card1Suit.equalsIgnoreCase("Diamonds")){
                    for (PokerCards.Number n : PokerCards.Number.values()) {     //GETS STUCK
                        if(n.name().equals(card1Num)) {
                            card1 = new PokerCards(PokerCards.Suit.Diamonds, PokerCards.Number.valueOf(card1Num));
                            System.out.println("Card 1 good to GO");
                            card1Good = true;
                        } else {
                            JFrame popUpError2 = new JFrame();
                            JOptionPane.showMessageDialog(popUpError2, "Incorrect Input, Number Input for First Card is incorrect! Please double check" +
                                    " your input", "ERROR", JOptionPane.ERROR_MESSAGE);
                            break;
                        }
                    }

Everything works lovely except the enhanced for-loop.除了增强的 for 循环外,一切都很好。 As i test other cards i get prompted by my error.当我测试其他卡时,我会收到错误提示。 It only accepts 'Ace', the first element.它只接受第一个元素“Ace”。 Why is my enhanced for-loop getting stuck?为什么我的增强型 for 循环卡住了?

The problem is that you should fail only if no cards match, not if only the first card doesn't, so you want to move the failing code to outside the loop like so:问题是你应该只在没有卡片匹配时失败,而不是只有第一张卡片不匹配,所以你想将失败的代码移到循环之外,如下所示:

for (PokerCards.Number n : PokerCards.Number.values()) {
    if(n.name().equals(card1Num)) {
        card1 = new PokerCards(PokerCards.Suit.Diamonds, PokerCards.Number.valueOf(card1Num));
        System.out.println("Card 1 good to GO");
        card1Good = true;
    }
}
if (!card1Good)
{
    JFrame popUpError2 = new JFrame();
    JOptionPane.showMessageDialog(popUpError2, "Incorrect Input, Number Input for First Card is incorrect! Please double check" +
                                    " your input", "ERROR", JOptionPane.ERROR_MESSAGE);
}

The for loop works fine, the if/else logic is the issue behind errors being thrown on cards other then 'Ace'. for 循环工作正常,if/else 逻辑是在“Ace”以外的卡片上抛出错误背后的问题。

As you mentioned yourself - if you use 'Ace' your code will work without error because 'Ace' is the first element in Number.values() and the check n.name().equals(card1Num) will be true , triggering the code in your if statement.正如您自己提到的那样 - 如果您使用“Ace”,您的代码将正常工作,因为“Ace”是Number.values()中的第一个元素,并且检查n.name().equals(card1Num)将为true ,触发if语句中的代码。 All other cards will fail on this and throw an error because they will be first matched against 'Ace'.所有其他卡片都将失败并抛出错误,因为它们将首先与“Ace”匹配。 If I understand correctly what you want to do I think you should use findAny() instead of your for loop.如果我正确理解您想做什么,我认为您应该使用findAny()而不是for循环。 The code would be something like this:代码将是这样的:

PokerCards.Number.values().stream().filter(number -> 
    number.name().equals(card1Num)).findAny().ifPresentOrElse(card ->  
        {your_if_logic_here}, 
        {your_else_logic_here})

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

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