Java登录管理系统

运用数组存放用户名和密码,数组下标一一对应,静态数组定义在类里面使外部函数能直接调用。

import java.util.Scanner;

public class ManageSystem {
    static Scanner scan = new Scanner(System.in);
    static String[] userNames = new String[10];
    static String[] passwords = new String[10];
    static int sum = 0;//用户数量

    public static void main(String[] args) {
        int i = 4;
        do {
            if (i == 4) {
                System.out.println("------ 黑马管理系统 ------");
                System.out.println("1.登录功能(login)");
                System.out.println("2.注册功能(register)");
                System.out.println("3.查看(show)");
                System.out.println("0.退出(exit)");
                System.out.println("------ 黑马管理系统 ------");
                System.out.print("请选择功能:");
                i = scan.nextInt();
            } else if (i == 1) {
                if (loginIn()) {
                    System.out.println("登陆成功!");
                } else {
                    System.out.println("登陆失败超过三次,已退出登陆页面!");
                }
                i = 4;
            } else if (i == 2) {
                if (!register()) {
                    i = 2;
                } else i = 4;
            } else if (i == 3) {
                System.out.println("系统中存在的用户名有:");
                showData();
                i = 4;
            } else i = 4;
        } while (i != 0);

    }

    public static Boolean loginIn() {//登录函数
        for (int chance = 0; chance < 3; chance++) {//三次机会,都不成功退出登陆界面
            System.out.print("请输入登陆用户名:");
            String userName = scan.next();
            System.out.print("请输入密码:");
            String password = scan.next();
            int j = findUserName(userName);
            if (passwords[j - 1].equals(password)) {
                return true;
            } else {
                System.out.println("密码错误。");
            }
        }
        return false;
    }

    public static boolean register() { //注册
        String password = null;
        for (int j = 0; j == 0; ) {
            System.out.print("请输入注册用户名:");
            String userName = scan.next();
            if (userName != null) {//判断用户名是否重复
                if (findUserName(userName) == 0) {
                    System.out.print("请输入密码:");
                    password = scan.next();
                    j++;
                } else {
                    System.out.println("用户名重复。");
                    return false;
                }
            }
            userNames[sum] = userName;
            passwords[sum] = password;
            System.out.println("注册成功");
            sum++;
        }
        return true;
    }

    public static void showData() {//选项三 展示所有数据
        for (int j = 0; j < sum; j++) {
            System.out.println((j + 1) + ". 用户名:" + userNames[j] + " 密码:" + passwords[j]);
        }
    }

    public static int findUserName(String u) {//寻找用户名(登录,注册)
        for (int j = 0; j < sum; j++) {
            if (userNames[j].equals(u)) {
                return j + 1;
            }
        }
        return 0;
    }
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值