简体   繁体   English

嵌套的 If-Else 语句

[英]Nested If-Else Statements

The code below is self explanatory.下面的代码是不言自明的。 Everything works fine.一切正常。 ( cb1 and cb2 are combo boxes). cb1cb2是组合框)。

HashMap<String,String> map = new HashMap<>();
ArrayList eight_to_nine = new ArrayList<>();
ArrayList nine_to_ten = new ArrayList<>();

private void btnActionPerformed(java.awt.event.ActionEvent evt) {

int time = cb1.getSelectedIndex();
String name = (String)cb2.getSelectedItem();
String a = map.get(name);

        
        if (time==0 && (!eight_to_nine.contains(name) || !eight_to_nine.contains(a))) {
            Collections.addAll(eight_to_nine, name, a);
            System.out.println("eight to nine: " + eight_to_nine);

        } else if (time==1 && (!nine_to_ten.contains(name) || !nine_to_ten.contains(a))){
            Collections.addAll(nine_to_ten, name, a);
            System.out.println("nine to ten: " + nine_to_ten);

        } else {
            JOptionPane.showMessageDialog(null, "Clash Detected!", "Error", JOptionPane.ERROR_MESSAGE);
        }
}

However, I want to add one more condition, which is each arraylist must not contain more than 5 elements.但是,我想再添加一个条件,即每个 arraylist 不得包含超过 5 个元素。 Something like:就像是:

if(eight_to_nine.size()>5 || nine_to_ten.size()>5) {
           JOptionPane.showMessageDialog(null, "Maximum Capacity Reached!", "Error", JOptionPane.ERROR_MESSAGE);
       }

But I am unsure where should I put this line.但我不确定我应该把这条线放在哪里。 I want the error to pop up without the arraylist being printed.我希望在不打印 arraylist 的情况下弹出错误。 And I am also unsure if I should use nested if-else or put everything in a While loop.而且我也不确定是否应该使用嵌套的 if-else 或将所有内容放在 While 循环中。 Any suggestions?有什么建议么?

With a bit of refactoring this is just one approach (may be affected by question in comments):通过一些重构,这只是一种方法(可能会受到评论中问题的影响):

private void btnActionPerformed(java.awt.event.ActionEvent evt) {
    
    //...existing code
    
    if (time == 0 && isUniqueTo(eight_to_nine,name,a)) {
        if (hasRoom(eight_to_nine,5)) {
            Collections.addAll(eight_to_nine, name, a);
            System.out.println("eight to nine: " + eight_to_nine);
        }
    }

    else if (time == 1 && isUniqueTo(nine_to_ten,name,a)) {
        if (hasRoom(nine_to_ten,5)) {
            Collections.addAll(nine_to_ten, name, a);
            System.out.println("nine to ten: " + nine_to_ten);
        }
    }

    else {
        JOptionPane.showMessageDialog(null, "Clash Detected!", "Error", JOptionPane.ERROR_MESSAGE);
    }
}

private boolean isUniqueTo(ArrayList al, String name, String a) {
    return !al.contains(name) || !contains(a)
}

private boolean hasRoom(ArrayList al, int max) {
    boolean result = (al.size() < max);
    if (al.size() == max) {
        JOptionPane.showMessageDialog(null, "Maximum Capacity Reached!", "Error", JOptionPane.ERROR_MESSAGE);   
    }
    return result;
        
}

i guess you can use a do while loop.我想你可以使用 do while 循环。 like喜欢

在此处输入图像描述

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

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