用户登录注册

定义一个用户类:
package AccountForUser;
public class Account {
String username;
String password;
String nickname;
String ChianeseName;
String tel;
String email;

public String toString(){
    return "用户名:"+username+","+"密码:"+password+","+"昵称"+nickname+","+"中文名"+ChianeseName+","+"手机号码"+tel+","+"邮箱"+email;
}

}
注册类,其中包含了很多方法,以及运用正则表达式进行的输入判断。
package AccountForUser;

import java.util.Scanner;

public class AccountRegist {
static Account a = new Account();
static AccountLoginTest aL=new AccountLoginTest();
static Account[] A=new Account[10];
int i=0;
public void register(Account m) {
acountname();
acountcode();
acountnickname();
acountchainesename();
acountTel();
acountemail();
A[i]=a;
i++;
aL.main(null);

}

public static void acountname() {
    System.out.println("请输入注册信息");
    while (true) {
        // 对用户名合法性进行判断
        Scanner sc = new Scanner(System.in);
        System.out.println("用户名");
        String s1 = sc.next();
        if (s1.matches("[0-9A-Za-z_]*")) {
            if (s1.length() < 16) {
                char c = s1.charAt(0);// 把字符串的第一个元素转换成char类型
                int i = (int) c;// 把char类型转换成int类型
                if ((i >= 65 && i <= 90) || (i >= 97 && i <= 122)) {
                    a.username = s1;
                    break;
                } else {
                    System.out.println("用户名格式错误:用户名的开头必须是字母。请重新输入!");
                    continue;
                }
            } else {
                System.out.println("用户名长度不能超过16个字符,请重新输入!");
                continue;
            }
        } else {
            System.out.println("用户名格式:由字母数字下划线组成,请重新输入!");
            continue;
        }
    }
}

// 对密码的格式进行判断
public static void acountcode() {
    while (true) {
        Scanner sc = new Scanner(System.in);
        System.out.println("密码:");
        String s2 = sc.next();
        boolean b = s2.matches("[0-9A-Za-z_]*");
        if (b = true) {
            if (s2.length() < 16) {
                a.password=s2;
                break;
            } else {
                System.out.println("密码不能超过16位");
                continue;
            }

        } else {
            System.out.println("密码格式:必须由字母和数字构成");
            continue;
        }
    }
}

// 昵称的设置
public static void acountnickname() {
    while (true) {
        System.out.println("昵称");
        Scanner sc = new Scanner(System.in);
        String s3 = sc.next();
        if (s3.matches("^[a-zA-Z0-9]{6,10}")) {
            if (s3.length() > 6 && s3.length() < 10) {
                a.nickname = s3;
                break;
            } else {
                System.out.println("昵称长度必须大于6小于10");
                continue;
            }

        } else {
            System.out.println("昵称格式:必须由字母和数字构成");
            continue;
        }
    }
}

// 手机号码的判断
public static void acountTel() {
    while (true) {
        System.out.println("手机号");
        Scanner sc = new Scanner(System.in);
        String s4 = sc.next();
        if (s4.matches("^[0-9]{11}")) {
            a.tel = s4;
            break;
        } else {
            System.out.println("手机号:全数字,长度为11位");
            continue;
        }
    }
}

// 中文名字的设置
public static void acountchainesename() {
    while (true) {
        System.out.println("中文名字:");
        Scanner sc = new Scanner(System.in);
        String s5 = sc.next();
        if (s5.matches("^[\u4E00-\u9FA5]{1,3}")) {
            a.ChianeseName = s5;
            break;
        } else {
            System.out.println("中文名字必须是中文");
            continue;
        }

    }
}

// 邮箱的设置
public static void acountemail() {
    while (true) {
        System.out.println("邮箱:");
        Scanner sc = new Scanner(System.in);
        String s6 = sc.next();
         if (s6.indexOf("@")>0&&s6.indexOf(".")<s6.length()&&s6.indexOf("@")+1<s6.indexOf("."))  {
            a.email = s6;
            System.out.println("注册成功");
            break;
        } else {
            System.out.println("邮箱格式不对");
            continue;
        }

    }
}

}
测试类:根据用户的选择进行登陆或注册,前面注册的用户全部保存在一个数组之中,之后只要注册过的用户均可以进行登陆。
package AccountForUser;

import java.util.Scanner;

public class AccountLoginTest {
static AccountRegist AR = new AccountRegist();

public static void main(String[] args) {
    System.out.println("------请选择业务:1.注册2.登陆-------");
    AccountRegist A = new AccountRegist();
    Account AC = new Account();
    Scanner sc = new Scanner(System.in);
    String i = sc.next();
    if (i.equals("1")) {
        A.register(AC);

    }
    if (i.equals("2")) {
        System.out.println("------请输入用户名和密码-------");
        System.out.println("用户名:");
        Scanner sc1 = new Scanner(System.in);
        String name = sc1.next();
        System.out.println("密码:");
        String code = sc1.next();

    for (int j = 0; j < AR.A.length; j++) {
            if (AR.A[j] != null) {
                if(AR.A[j].username.equals(name)&&AR.A[j].password.equals(code)){
                System.out.println(AR.A[j]);
                System.out.println("登陆成功!");
                break;

                }else{
                    continue;
                }

            }else{
                System.out.println("登录失败!");
            }
        }
    }
}

}
欢迎指正!

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值