//有点懒,没放截图运行效果。如果想要源码的,或者有看不懂的地方可以留言,或者加我qq 572739652私聊我。
初学练手之作,方法比较蠢,请大佬们多多包涵。
package login;
import java.util.Scanner;
public class user {
private String userName;
private String password;
private boolean isDenglu;
public user (){
isDenglu=false;
}
public void input(){
Scanner sc=new Scanner(System.in);
String userName,password;
System.out.println("请输入用户名密码:");
userName=sc.next();
password=sc.next();
this.userName=userName;
this.password=password;
}
public String getUserName() {
return userName;
}
public String getPassword() {
return password;
}
public boolean isDenglu() {
return isDenglu;
}
}
package login;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
public class login {
private Map map;
public login(){
map=new HashMap();
}
public boolean zhuce(user u){
if(map.containsKey(u.getUserName())==true){
return false;}
// Map map1=new HashMap();
map.put(u.getUserName(), u.getPassword());
// String password;
// System.out.println("请再次输入密码确定:");
// Scanner sc=new Scanner();
// password=sc.next();
System.out.println("您已经成功注册!");
return true;
}
public boolean denglu(user u){
if(map.containsKey(u.getUserName())==false){
System.out.println("该用户不存在!请注册!");return false;
}
else if(map.get(u.getUserName()).equals(u.getPassword())){
System.out.println("登陆成功!");
//u.is_denglu(true);
return true;
}
else System.out.println("密码不正确,登录失败!");
return false;
}
public StringBuffer random_code(){ //随机生成验证码
StringBuffer sf=new StringBuffer("");
Random select=new Random();
for(int i=0;i<4;i++){
int num=select.nextInt(3-1+1)+1;
if(num==1){
char one=(char)(select.nextInt('9'-'0'+1)+'0');
sf.append(one);
}
if(num==2){
char two=(char)(select.nextInt('Z'-'A'+1)+'A');
sf.append(two);
}
if(num==3){
char three=(char)(select.nextInt('z'-'a'+1)+'a');
sf.append(three);
}
}
return sf;
}
public boolean isRightRandom_code(StringBuffer sf,String input){
if(input.length()==4){
String one=input.substring(0, 1);
if(one.charAt(0)>='A' && one.charAt(0) <='Z'){one=one.toLowerCase();}
String two=input.substring(1, 2);
if(two.charAt(0)>='A' && two.charAt(0) <='Z'){two=two.toLowerCase();}
String three=input.substring(2, 3);
if(three.charAt(0)>='A' && three.charAt(0) <='Z'){three=three.toLowerCase();}
String four=input.substring(3, 4);
if(four.charAt(0)>='A' && four.charAt(0) <='Z'){four=four.toLowerCase();}
StringBuffer inputBF=new StringBuffer("");
inputBF.append(one);
inputBF.append(two);
inputBF.append(three);
inputBF.append(four); //把输入的字符串逐个截取并转化为小写
String one1=sf.substring(0, 1);
if(one1.charAt(0)>='A' && one1.charAt(0) <='Z'){one1=one1.toLowerCase();}
String two1=sf.substring(1, 2);
if(two1.charAt(0)>='A' && two1.charAt(0) <='Z'){two1=two1.toLowerCase();}
String three1=sf.substring(2, 3);
if(three1.charAt(0)>='A' && three1.charAt(0) <='Z'){three1=three1.toLowerCase();}
String four1=sf.substring(3, 4);
if(four1.charAt(0)>='A' && four1.charAt(0) <='Z'){four1=four1.toLowerCase();}
StringBuffer sfBF=new StringBuffer("");
sfBF.append(one1);
sfBF.append(two1);
sfBF.append(three1);
sfBF.append(four1); //原字符转化为小写
if(inputBF.toString().equals(sfBF.toString())==true){return true;}
}
return false;
}
public void getuser(){
Scanner sc=new Scanner(System.in);
String admin,password;
admin=sc.next();
password=sc.next();
if(admin.equals("胡双阳")==true&& password.equals("572739652")==true){
System.out.println("====================================");
System.out.println("管理员账户登陆成功!已经注册的用户有:");
for(Object i:map.keySet()){
System.out.println(i);
}
}
else System.out.println("账户或密码错误,管理员登录失败!");
}
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
boolean flag=true,exist_user=false;
login person=new login();
while(flag){
StringBuffer bf=new StringBuffer(person.random_code());
System.out.println("=========================");
System.out.println("\t验证码:"+bf);
System.out.println("=========================");
String input;
input=sc.next();
if(person.isRightRandom_code(bf, input)==true){System.out.println("验证码匹配成功!");flag=false;}
else System.out.println("验证码匹配失败,请重新输入!");
}
flag=true;
int n;
while(flag){
System.out.print("输入1注册 输入2登录 输入3登录管理员账号查看已注册用户 输入4注销已登录账户 输入0退出:");
n=sc.nextInt();
if(n==4){exist_user=false;}
if(exist_user==true){System.out.println("已有用户登录,无法进行其他操作!请先注销");continue;}
if(n==0){System.out.println("感谢使用!");flag=false;}
else if(n==1){
user user1=new user();
user1.input();
if(person.zhuce(user1)==false)
{System.out.println("该用户名已经被占用!请重新注册或者选择登录");}
else {person.zhuce(user1); }}
else if(n==2){
user user1=new user();
user1.input();
//person.denglu(user1);
if(person.denglu(user1)==true){exist_user=true;}
}
else if(n==3){person.getuser();}
else if(n==4){exist_user=false;System.out.println("注销成功!");}
else System.out.println("操作有误!");
}
}
}