package jul29.huiyuanguanli; import java.util.*; //会员类 class User { private String name; private String code; public User() { } public User(String name, String code) { this.name = name; this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } } //会员注册 public class Demo01 { static Scanner input = new Scanner ( System.in ); //储存会员类 static Map<String, User> map = new HashMap<> (); static long time1 = 0;//输入错误3次的时间 static long tiem2 = 0;//输入密码时间 static int num;//输错密码的次数 static final long time = 15;//锁死账号的时间 //注册 public static void zc() { System.out.println ( "请输入用户名:" ); String name; for (name = input.next (); name.length () < 3; name = input.next ()) { System.out.println ( "用户名长度不能小于三位,请重新输入:" ); } System.out.println ( "请输入注册密码:" ); String code; for (code = input.next (); code.length () < 6; code = input.next ()) { System.out.println ( "密码长度不能小于6位,请重新输入:" ); } System.out.println ("请再次输入注册密码:"); String code2 = input.next (); while(!code2.equals ( code )){ System.out.println ("您两次输入的密码不相同,请重新输入:"); code2 = input.next (); } System.out.println ( "注册成功!" ); //存储会员 map.put ( name, new User ( name, code ) ); } //登入 public static void dr() { tiem2 = new Date ().getTime (); //先判断输错次数,再判断时间是否还在锁死时间内,最后就剩被已锁死状态 if (num < 3) { //输对登入成功,输错则次数+1 if (boo ()) { System.out.println ( "登入成功!" ); } else { num++; System.out.println ( "密码输入错误,您当前已输错" + num+"次,输错3次将暂时不能登入!" ); //输错3次,记录当时的时间 if (num == 3) { time1 = new Date ().getTime (); } } } else if ((tiem2 - time1) / 1000 > time) { //时间已过,清除输错次数 num = 0; if (boo ()) { System.out.println ( "登入成功!" ); } else { num++; System.out.println ( "密码输入错误,您当前已输错\"+num\"次,输错3次将暂时不能登入!" ); } } else { System.out.println ( "您已输错3次,请" + (time - (tiem2 - time1) / 1000) + "秒后重试" ); } } //判断是否输入正确 public static boolean boo() { System.out.println ( "请输入账号:" ); String name = input.next (); System.out.println ( "请输入密码:" ); String code = input.next (); if (map.get ( name ) != null) { if (name.equals ( map.get ( name ).getName () ) && code.equals ( map.get ( name ).getCode () )) { return true; } else { return false; } } else { return false; } } //运行 public static void main(String[] args) { String s = ""; System.out.println ( "1、登入 2、注册\n请选择:" ); for (s = input.next (); s.equals ( "1" ) || s.equals ( "2" ); s = input.next ()) { switch (s) { case "1": dr (); break; case "2": zc (); break; } System.out.println ( "1、登入 2、注册\n请选择:" ); } } }
输错3次且已过15秒效果