我有以下代码,我试图在链接列表中存储用户名和密码。根据用户从扫描仪输入的内容进行身份验证,但似乎无法使其正常工作。任何肝脏都会受到赞赏。
import java.io.IOException;
import java.util.LinkedList;
//Linked lists declared to store, auctions, users, and items
public class System1{
private static LinkedList<User> users = new LinkedList<User>();
public static String username;
public static String password;
public static void main(String[] args) throws IOException, InterruptedException {
//User.setuser();
String username = "user";
String password = "pw123";
users.add(new User("user", "pw123"));
users.add(new User("user1", "pw123"));
users.add(new User("user2", "pw123"));
User.validateLogIn(users, username, password);
System.out.println("If true you are logged in:" + User.userLogedIn);
第二课
public class User {
public User(String username, String password) {
}
static boolean userLogedIn;
public static void validateLogIn( LinkedList<User> users, String username, String password) {
Iterator<User> spin = users.iterator();
while (spin.hasNext()){
User user = spin.next();
//System.out.println("Searching");
//System.out.println(username);
if(username.equals(user.setUsername()) && password.equals(user.setPassword())) {
//System.out.println("BINGO!");
userLogedIn=true;
} else {
//return false;
//System.out.println("User not found" + "\n");
}
}
}
private String setUsername() {
String username = "user1";
System.out.println("Enter your username: ");
Scanner scanner = new Scanner(System.in);
username = scanner.nextLine();
return username;
}
private String setPassword() {
String password = "pw123";
return password ;
}
/*public static void setuser(){
System.out.println("Enter your username: ");
Scanner scanner = new Scanner(System.in);
System1.username = scanner.nextLine();
System.out.println("Your username is " + System1.username);
System.out.println("Enter your password: ");
Scanner scanner1 = new Scanner(System.in);
System1.password = scanner1.nextLine();
System.out.println("Your password is " + System1.password);
}*/
答案 0 :(得分:0)
首先,您需要存储添加到链接列表的用户名和密码(在“用户类”构建器中)
其次,您在验证时尝试做的事情并不清楚。你不需要传递所有这些参数,LinkedList id就足够了。
这样的事情: System1.java:
import java.io.IOException;
import java.util.LinkedList;
//Linked lists declared to store, auctions, users, and items
public class System1{
private static LinkedList<User> users;
public static void main(String[] args) throws IOException, InterruptedException {
users = new LinkedList<User>()
users.add(new User("user", "pw123"));
users.add(new User("user1", "pw123"));
users.add(new User("user2", "pw123"));
User.validateLogIn(users);
System.out.println("If true you are logged in:" + User.userLogedIn);
}
User.java:
import java.util.Scanner;
公共类用户{
private String userName;
private String password;
public static boolean userLogedIn;
public User(String username, String password) {
this.userName = username;
this.password = password;
}
public static void validateLogIn( LinkedList<User> users) {
userLogedIn = false;
String usernameToCheck, passwordToCheck;
System.out.println("Enter your username: ");
Scanner scanner = new Scanner(System.in);
usernameToCheck = scanner.nextLine();
System.out.println("Enter your password: ");
passwordToCheck = scanner.nextLine();
Iterator<User> spin = users.iterator();
while (spin.hasNext()){
User user = spin.next();
//System.out.println("Searching");
//System.out.println(username);
String userToCheck
if(user.userName.equals(userToCheck) && user.password.equals(passwordToCheck)) {
//System.out.println("BINGO!");
userLogedIn=true;
} else {
//return false;
//System.out.println("User not found" + "\n");
}
}
}