首页 > 代码库 > 输入用户名查询是否存在

输入用户名查询是否存在

 1 import java.util.HashMap; 2 import java.util.Map; 3 import java.util.Scanner; 4 import java.util.Set; 5  6 public class UserTest { 7  8     public static void main(String[] args) { 9         Scanner sc=new Scanner(System.in);10         System.out.println("请输入用户名:");11         String uname=sc.next();12         System.out.println("请输入密码:");13         String passwd=sc.next();14         User u=new User(uname,passwd);15         Map<String,User> map=new HashMap<String,User>();16         map.put("张三",new User("张三","123"));17         map.put("李四",new User("李四","152"));18         map.put("王五",new User("王五","432"));19         map.put("赵六",new User("赵六","142"));20         map.put("唐七",new User("唐七","846"));21         Set<Map.Entry<String,User>> entries=map.entrySet();22         boolean flags=false;23         for(Map.Entry<String,User> e:entries){24             if(e.getKey().equals(uname)&&e.getValue().getUsername().equals(uname)&&e.getValue().getPassword().equals(passwd)){//&&e.getValue().equals(e)25                 System.out.println("含有该用户的信息!");26                 flags=true;27                 break;28             }29         }30         if(flags==false){31             System.out.println("系统未查询到该用户的信息!");32         }33     }34 }35 36 class User {37     String username;38     String password;39 40     public User(String username, String password) {41         super();42         this.username = username;43         this.password = password;44     }45 46     public String getUsername() {47         return username;48     }49 50     public void setUsername(String username) {51         this.username = username;52     }53 54     public String getPassword() {55         return password;56     }57 58     public void setPassword(String password) {59         this.password = password;60     }61 62     @Override63     public String toString() {64         return "User [username=" + username + ", password=" + password + "]";65     }66 67 }

技术分享

技术分享

 

输入用户名查询是否存在