我要创建一个用于雇员系统的登录版本,我有一个for循环,该循环应遍历整个帐户列表,然后查看雇员名称是否与列表中的一个匹配,然后if语句继续,再问其他问题,等等。。。它似乎只迭代一次,然后停止,因为它只会找到第一个用户,并告诉我其他帐户不存在,即使它们存在!!我究竟做错了什么?我的列表还包含从Account继承的雇员和经理,if语句使用Account中的getName来比较它是否等于用户输入。抱歉,这很荒谬/糟糕!谢谢。
List <Account> Accounts = new LinkedList<Account>();
这是我填充帐户的地方,主要方法调用此方法,list()称为whihc包含问题循环
public void add() {
Employee Geoff = new Employee("Geoff", "password1");
Manager Bob = new Manager("Bob", "password2");
Employee John = new Employee("John", "password3");
Accounts.add(Geoff);
Accounts.add(Bob);
Accounts.add(John);
list();
}
问题:
System.out.println("Hello welcome: ");
System.out.println("Please enter your name: ");
String empName = Scan.nextLine();
for (Account a : Accounts) {
System.out.println(a);
if (a.getname().equals(empName)) {
System.out.println("\nPlease enter your passcode: ");
String code = Scan.nextLine();
if (a.check(code) == true) {
System.out.println("logged in");
}
}
System.out.println("Employee does not exist!");
login();
}
我正在for循环中执行print语句,以查看它是什么,但不幸的是,这只是第一个帐户
编辑:我在这里包括更多的代码,我最初的if语句后,我想检查用户输入的代码是否正确。
看看雇员的姓名是否与列表中的一个匹配,然后if语句继续,进一步的问题等等。。。它似乎只重复一次然后停止,因为它只会找到第一个用户,告诉我其他帐户不退出,即使他们这样做!!
它是否适合一个员工,并告诉别人不要的环路,则你的存在确实不迭代一次。
您得到的输出正是代码的样子。您一次获得用户名,然后尝试将相同的名称与列表中的每个员工匹配。如果名称相等,则要求输入密码,否则您将打印出该员工不存在。一切都正确,如代码中所示。您应该在问题中添加预期的行为,以便我或其他人可以在不猜测方法目的的情况下修复您的代码。
这是这些猜测之一:
System.out.println("Please enter your name: ");
String empName = Scan.nextLine();
boolean userFound = false;
for (Account a : Accounts) {
System.out.println(a);
if (a.getname().equals(empName)) {
System.out.println("\nPlease enter your passcode: ");
String code = Scan.nextLine();
if (a.check(code) == true) {
System.out.println("logged in");
userFound = true;
break;
}
}
}
if(userFound) {
login();
} else {
System.out.println("User not found.");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句