import java.util.HashMap;
import java.util.Scanner;
//散列表演示
public class HashMapDemo {
public static void main(String[] args) {
HashMap users=new HashMap();
//注册用户
users.put("Tom", new User("Tom","123",5));
users.put("Jack", new User("Jack","124",4));
users.put("Danny", new User("Danny","125",3));
System.out.println(users);
//登录查找
Scanner s=new Scanner(System.in);
while(true){
System.out.println("用户名:");
String name=s.nextLine();
System.out.println("密码:");
String pwd=s.nextLine();
if(!users.containsKey(name)){
System.out.println("没有注册");
continue;
}
User user=(User)users.get(name);
if(user.pwd.equals(pwd)){
System.out.println("欢迎"+user.name+" age"+user.age);
break;
}
}
}
}
class User {
String name;
String pwd;
int age;
public User(String name,String pwd,int age){
this.name=name;
this.age=age;
this.pwd=pwd;
}
public String toString(){
return name+":"+age;
}
}
HashSet------简单实现用户登录
最新推荐文章于 2022-05-27 18:09:08 发布