英文:
How to reset the loop when the loop found the matched username and password?
问题
public void actionPerformed(ActionEvent e){
if(e.getSource() == loginButton){
for(int i = 0; i < AcademicSystem.allUser.size();){
User usr = AcademicSystem.allUser.get(i);
User pswrd = AcademicSystem.allUser.get(i);
System.out.println(usr.getLogin_username() + "\t" + userName.getText());
System.out.println(pswrd.getLogin_password() + "\t" + password.getText());
System.out.println("");
if(userName.getText().equals(usr.getLogin_username()) && password.getText().equals(pswrd.getLogin_password())){
JOptionPane.showMessageDialog(loginButton, "Username and password matched!");
JOptionPane.showMessageDialog(loginButton, "User[" + userName.getText() + "] have successfully logged in");
Dashboard db = new Dashboard();
setVisible(false);
db.setVisible(true);
break;
}
else if (!AcademicSystem.allUser.contains(userName.getText()) && !AcademicSystem.allUser.contains(password.getText())){
i++;
}
}
}
}
英文:
This code section is part of code in my program. I am currently write a GUI program. This part of code is about to extract the username and password from the ArrayList and if matched then user are allowed to login. My desired output is if this loop find the matched username and password, it will break and proceed to another interface; Oppositely, if didn't matched, the loop will continue to find the matched one. So then, how should my if-else going to be ? I can't pop up the message that show username and password is wrong because in else block, I need it to loop until find the matched.
public void actionPerformed(ActionEvent e){
if(e.getSource() == loginButton){
for(int i =0;i<AcademicSystem.allUser.size();){
User usr = AcademicSystem.allUser.get(i);
User pswrd = AcademicSystem.allUser.get(i);
System.out.println(usr.getLogin_username() + "\t" + userName.getText());
System.out.println(pswrd.getLogin_password() + "\t" + password.getText());
System.out.println("");
if(userName.getText().equals(usr.getLogin_username())&&password.getText().equals(pswrd.getLogin_password())){
JOptionPane.showMessageDialog(loginButton,"Username and password matched!");
JOptionPane.showMessageDialog(loginButton, "User[" + userName.getText() + "] have successfully logged in");
Dashboard db = new Dashboard();
setVisible(false);
db.setVisible(true);
break;
}
else if (!AcademicSystem.allUser.contains(userName.getText())&&!AcademicSystem.allUser.contains(password.getText())){
i++;
}
}
}
}
答案1
得分: 2
不要试图在循环中处理所有事情。在循环结束之前,您实际上不知道应该采取什么操作,因此,相反地,遍历可用的用户并找到与条件匹配的用户,如果找不到,让匹配保持为 "null"。
然后在循环结束时,您可以检查 "matched" 对象的状态并采取适当的操作。
例如:
User matchedUser = null;
String userName = this.userName.getText();
String password = this.password.getText();
for (User user : AcademicSystem.allUser) {
if (userName.equals(usr.getLogin_username()) && password.equals(pswrd.getLogin_password())) {
matchedUser = user;
break;
}
}
if (matchedUser != null) {
// 操作成功
} else {
// 验证失败
}
这种方法可以将逻辑转移到 "AcademicSystem" 中,并且可以减少对以下类似内容的调用:
User matchedUser = AcademicSystem.authenticate(userName.getText(), password.getText());
if (matchedUser != null) {
// 操作成功
} else {
// 验证失败
}
这将把验证用户的责任移交到其所属的领域,并在此过程中减少代码复杂性,双赢。
英文:
Don't try and do everything in you loop. Until the loop ends, you don't actually know what action you should take, so, instead, loop through the available users and find one which matches the criteria or, if you can't, let the match remain null
.
Then when the loop ends, you can check the state of the matched
object and take appropriate action.
For example:
User matchedUser = null;
String userName = this.userName.getText();
String password = this.password.getText();
for (User user : AcademicSystem.allUser) {
if (userName.equals(usr.getLogin_username()) && password.equals(pswrd.getLogin_password())) {
matchedUser = user;
break;
}
}
if (matchedUser != null) {
// You're good to go
} else {
// Authentication failed
}
This approach would all you to move the logic to the AcademicSystem
and would then allow you to reduce the call to something more like:
User matchedUser = AcademicSystem.authenticate(userName.getText(), password.getText());
if (matchedUser != null) {
// You're good to go
} else {
// Authentication failed
}
This moves the responsibility for validating the user to the domain it belongs in and reduces you overall code complexity in the process, win/win
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论