如何在 get 方法和 if 语句中使用不等于(not equals)操作符。

huangapple go评论74阅读模式
英文:

How can I use not equals in the get method and if statement

问题

我正在尝试遍历我的客户列表,查看个人号码是否匹配,否则应该抛出一个错误,说明客户不存在。但是即使存在客户,我始终收到客户不存在的错误消息。

我的方法:

public ArrayList<String> getCustomer(String pNo){
   ArrayList<String> custInfo = new ArrayList<String>();
     for(int i=0; i<customerList.size(); i++){
        if(customerList.get(i).getPersonalNumber().equals(pNo)){
        custInfo.add(customerList.get(i).getCustomerInfo());
        for(int j = 0; j<customerList.get(i).getAllAccounts().size(); j++){
            custInfo.add(customerList.get(i).getAllAccounts().get(j).getAccount());
        }
        return custInfo;
        }
       if (!customerList.get(i).getPersonalNumber().equals(pNo)) {
    	JOptionPane.showMessageDialog(null, "There is no such customer", "Error", JOptionPane.PLAIN_MESSAGE);
    }
    
}
return null;
}

当我在第一个if之后写上else,它做的事情是一样的。

英文:

I am trying to loop through my customer list and see if the personal numbers match, otherwise it should throw an error that the customer doesn't exist. But I always get the error message that customer doesn't exist even when it does.

My method:

public ArrayList&lt;String&gt; getCustomer(String pNo){
   ArrayList&lt;String&gt; custInfo = new ArrayList&lt;String&gt;();
     for(int i=0; i&lt;customerList.size(); i++){
        if(customerList.get(i).getPersonalNumber().equals(pNo)){
        custInfo.add(customerList.get(i).getCustomerInfo());
        for(int j = 0; j&lt;customerList.get(i).getAllAccounts().size(); j++){
            custInfo.add(customerList.get(i).getAllAccounts().get(j).getAccount());
        }
        return custInfo;
        }
       if (!customerList.get(i).getPersonalNumber().equals(pNo)) {
    	JOptionPane.showMessageDialog(null, &quot;There is no such customer&quot;, &quot;Error&quot;,       JOptionPane.PLAIN_MESSAGE);
    }
    
}
return null;

}

When I write else after first if it does the same thing

答案1

得分: 3

你正在检查个人号码是否在每次迭代中匹配。即使第一个客户不匹配,您也会显示对话框,即使第二个客户匹配。

由于在 if 语句内部有一个 return 语句,当找到匹配的客户时,执行将退出循环。您只需将 showMessageDialog 移动到循环之后,仅当未找到匹配的客户时才会运行:

public ArrayList<String> getCustomer(String pNo) {
    ArrayList<String> custInfo = new ArrayList<String>();
    for (int i = 0; i < customerList.size(); i++) {
        if (customerList.get(i).getPersonalNumber().equals(pNo)) {
            custInfo.add(customerList.get(i).getCustomerInfo());
            for (int j = 0; j < customerList.get(i).getAllAccounts().size(); j++) {
                custInfo.add(customerList.get(i).getAllAccounts().get(j).getAccount());
            }
            return custInfo;
        }
    }
    JOptionPane.showMessageDialog(null, "There is no such customer", "Error", JOptionPane.PLAIN_MESSAGE);
    return null;
}
英文:

You are checking if the personal number matches on every iteration. If the first customer does not match, you show the dialog, even if the second customer did match.

Since you have a return statement within the if-statement, the execution exits the loop when a matching customer is found. You can simply move showMessageDialog until after the loop, and it will run only when a matching customer is not found:

public ArrayList&lt;String&gt; getCustomer(String pNo) {
    ArrayList&lt;String&gt; custInfo = new ArrayList&lt;String&gt;();
    for (int i = 0; i &lt; customerList.size(); i++) {
        if (customerList.get(i).getPersonalNumber().equals(pNo)) {
            custInfo.add(customerList.get(i).getCustomerInfo());
            for (int j = 0; j &lt; customerList.get(i).getAllAccounts().size(); j++) {
                custInfo.add(customerList.get(i).getAllAccounts().get(j).getAccount());
            }
            return custInfo;
        }
    }
    JOptionPane.showMessageDialog(null, &quot;There is no such customer&quot;, &quot;Error&quot;, JOptionPane.PLAIN_MESSAGE);
    return null;
}

huangapple
  • 本文由 发表于 2020年8月14日 21:42:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63413944.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定