停止在Java中循环

时间:2017-12-02 11:24:16

标签: java

如何阻止它循环?关于无效用户名和密码的错误消息框将根据我在文本文件中的用户数量弹出,我不知道如何解决它。

for (int i = 0; i < userList.size(); i++) {
            if (userList.get(i).getUserID().equals(txtUserID.getText()) && userList.get(i).getPassword().equals(ptxtPassword.getText())) {
                if (userList.get(i).getUserType().equals("Administrator")) {
                    System.out.println("Welcome Admin " + userList.get(i).getName() + " (" + userList.get(i).getUserID() + ")");
                    mainMenuForm.lblUser.setText("Welcome Admin " + userList.get(i).getName() + " (" + userList.get(i).getUserID() + ")");

                    mainMenuForm.setVisible(true);

                    this.setVisible(false);

                } else if (userList.get(i).getUserType().equals("Sales Manager")) {
                    System.out.println("Welcome SM" + userList.get(i).getName());
                    mainMenuForm.lblUser.setText("Welcome SM " + userList.get(i).getName() + " (" + userList.get(i).getUserID() + ")");

                    mainMenuForm.setVisible(true);
                    mainMenuForm.lblRegistration.hide();

                    this.setVisible(false);

                } else if (userList.get(i).getUserType().equals("Purchase Manager")) {
                    System.out.println("Welcome PM" + userList.get(i).getName());
                    mainMenuForm.lblUser.setText("Welcome PM " + userList.get(i).getName() + " (" + userList.get(i).getUserID() + ")");

                    mainMenuForm.setVisible(true);
                    mainMenuForm.lblDailySales.hide();

                    this.setVisible(false);
                }
            } else {
                JOptionPane.showMessageDialog(null, "Invalid User ID or Password!", "Login Error", JOptionPane.OK_OPTION);
            }
        }

4 个答案:

答案 0 :(得分:1)

为了摆脱java中的循环,break是使用的命令。

你想要的是伪代码:

credentialsCorrect = false;
loop(usersList) {
  if (correct credentials) {
    credentialsCorrect = true;
    break;
  }
}
if (credentialsCorrect) {
  showCorrectCredentialsResponse();
} else {
  showWrongCredentialsResponse();
}

或者甚至更好,以用户ID为关键字的地图,因此您不需要进行手动循环:

userData = usersMap.get(userId);
credentialsCorrect = userData != null && matches(userData, password)
if (credentialsCorrect) {
  showCorrectCredentialsResponse();
} else {
  showWrongCredentialsResponse();
}

答案 1 :(得分:1)

如果我正确理解了您的问题,您可以使用break关键字来摆脱循环。如果您想整理代码,可以使用if语句替换switch语句,以提高可读性。

答案 2 :(得分:0)

摆脱

for (int i = 0; i < userList.size(); i++) {

阻止它为每个用户循环

答案 3 :(得分:0)

您可以使用break声明,但我会反对它。查看this answer了解详情。

您在for循环中显示消息。您可以将其移出并控制您是否使用boolean变量找到了用户/密码。如果没有这样的用户/密码组合,则该消息应仅可见一次:

boolean found = false;
for (int i = 0; i < userList.size() && !found; i++) {
    if (userList.get(i).getUserID().equals(txtUserID.getText()) && userList.get(i).getPassword().equals(ptxtPassword.getText())) {
        found = true;
        if (userList.get(i).getUserType().equals("Administrator")) {
            System.out.println("Welcome Admin " + userList.get(i).getName() + " (" + userList.get(i).getUserID() + ")");
            mainMenuForm.lblUser.setText("Welcome Admin " + userList.get(i).getName() + " (" + userList.get(i).getUserID() + ")");

            mainMenuForm.setVisible(true);

            this.setVisible(false);

        } else if (userList.get(i).getUserType().equals("Sales Manager")) {
            System.out.println("Welcome SM" + userList.get(i).getName());
            mainMenuForm.lblUser.setText("Welcome SM " + userList.get(i).getName() + " (" + userList.get(i).getUserID() + ")");

            mainMenuForm.setVisible(true);
            mainMenuForm.lblRegistration.hide();

            this.setVisible(false);

        } else if (userList.get(i).getUserType().equals("Purchase Manager")) {
            System.out.println("Welcome PM" + userList.get(i).getName());
            mainMenuForm.lblUser.setText("Welcome PM " + userList.get(i).getName() + " (" + userList.get(i).getUserID() + ")");

            mainMenuForm.setVisible(true);
            mainMenuForm.lblDailySales.hide();

            this.setVisible(false);
        }
    }
}

// if we didn't find the user/password combination, display the error message
if(!found) {
    JOptionPane.showMessageDialog(null, "Invalid User ID or Password!", "Login Error", JOptionPane.OK_OPTION);
}