注册登录,基本操作满足,部分知识还需补习
package cn.limitinglogin; //会员类 public class Users { private String name; private String password; public Users() {} public Users(String name, String password) { super(); this.name = name; this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
package cn.limitinglogin; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.Scanner; //后台模块类 public class Background { //储存会员信息 static Scanner sc = new Scanner(System.in); static Map<String,Users>map = new HashMap<>(); static long time1 = 0;//输错3次密码时的时间 static long time2 = 0;//输入密码时间 static int num;//密码输入错误的次数 static final long time = 20;//账号不能登录的时间 //会员注册部分 public static void Loginz() { System.out.println("请输入用户昵称:"); String name = sc.next(); for(;name.length()<3;name = sc.next()) { //p50 System.out.println("用户名长度不能小于三位,请重新输入:"); } System.out.println("请输入注册密码:"); String password = sc.next(); for(;password.length()<6;password = sc.next()) { System.out.println("密码长度小于6位,请重新输入:"); } System.out.println("请再一次输入密码:"); String password1 = sc.next(); while(!password1.equals(password)) { System.out.println("两次密码不相同,请重新输入:"); password1 = sc.next(); } System.out.println("注册成功"); //将会员信息存进去 map.put(name, new Users(name,password)); } //会员登录部分 public static void Logind() { time2 = new Date().getTime(); if(num < 3) { if(flag()) { System.out.println("登入成功"); }else { num++; System.out.println("密码输入错误,您当前已经输错"+num+"次,输错3次将暂时不能登录");//?? //记录此时的时间 if(num==3) { time1 = new Date().getTime(); } } }else if((time1-time2)/1000>time) {//?? //到达时间后3次被清除 num = 0; if(flag()) { System.out.println("登陆成功"); }else { num++; System.out.println("密码输入错误,您当前已经输错"+num+"次,输错3次将暂时不能登录"); } }else { System.out.println("您已经输错3次,请"+(time - (time2 - time1) / 1000)+"秒后重试"); } // System.out.println("您已经输错3次请在"); } //判断输入是否正确 public static boolean flag() { System.out.println("请输入昵称:"); String name = sc.next(); System.out.println("请输入密码:"); String password = sc.next(); if(map.get(name)!=null) { if(name.equals(map.get(name).getName())&&password.equals(map.get(name).getPassword())) { return true; } else { return false; } }else { return false; } } }
package cn.limitinglogin; import java.util.Scanner; //测试类 public class Test { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { Background bg = new Background (); String xx = ""; System.out.print("1、注册"+"\t"+"2、登录"+"\t"+"请输入你的选择:"); xx = sc.next(); for(;xx.equals("1")||xx.equals("2");xx = sc.next()) { switch(xx) { case "1": bg.Loginz();//?怎么别人可以之间.出来 break; case "2": bg.Logind(); break; } System.out.print("1、注册"+"\t"+"2、登录"+"\t"+"请输入你的选择:"); } } }