• java入门,登录注册案例


    java入门,登录注册案例

    任务描述

    编写程序实现简单的登录注册功能。程序包含以下4个功能:
    (1)登录功能,用户输入正确的账号密码进行成功:
    (2)注册功能,输入用户名和密码进行注册
    (3)查看功能,查看所有的用户名与密码;
    (4)退出功能,退出系统。
    用户可以输入对应的编号进行相应的功能操作。例如,输入2进入注册功能,输入用户名和密码进行注册。

    主程序和类变量
        static Scanner sc = new Scanner(System.in);
        static String[] arrayname = new String[10];
        static String[] arraypass = new String[10];
        static int num=0;
        public static void main(String[] args) {
    
            while (true) {
                System.out.println("请选择功能:1.登录   2 注册  3.查看  4.退出" );
                int select = sc.nextInt();
                switch (select)
                {
                    case 1:
                        login();
                        break;
                    case 2:
                        register();
                        break;
                    case 3:
                        show_user();
                        break;
                    case 4:
                        return ;
                }
            }
        }
      
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    登录程序
        public static void login()
        {
            System.out.println("欢迎使用登录功能");
            System.out.println("用户名:");
            String username = sc.next();
            System.out.println("密码:");
            String userpass = sc.next();
    
    //        与已有数据进行比对
            int status =0;
            for (int i = 0; i < arrayname.length; i++) {
                if (username.equals(arrayname[i]))
                {
                    if (userpass.equals(arraypass[i]))
                    {
                        System.out.println("登录成功");
                    }
                    else {
                        System.out.println("登录失败,账号或密码错误");
                    }
                    status++;
                }
            }
            if (status == 0)
            {
                System.out.println("未查询到该用户,请前往注册");
            }
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    注册
        public static void register()
        {
            System.out.println("欢迎使用注册功能");
            System.out.println("用户名:");
            String username = sc.next();
    
            int is_no =0;//判断是否存在 0不存在 大于0存在
            for (int i = 0; i < arrayname.length; i++) {
                if (username.equals(arrayname[i]))
                {
                    is_no++;
                }
            }
            if (is_no > 0)
            {
                System.out.println("该用户已经注册,请勿重复注册");
            }
            else
            {
                arrayname[num]  = username;
                System.out.println("密码:");
                String userpass = sc.next();
                arraypass[num] = userpass;
                num++;
                System.out.println("注册成功");
            }
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    展示
       public static void show_user()
        {
            if (num ==0) //判断数据是否为空
            {
                System.out.println("暂无数据");
            }
            else{
                for (int i = 0; i < num; i++) {
                    System.out.print("用户名");
                    System.out.print(arrayname[i]);
                    System.out.print("密码");
                    System.out.println(arraypass[i]);
                }
            }
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    全部代码

    记得先创建项目

        static Scanner sc = new Scanner(System.in);
        static String[] arrayname = new String[10];
        static String[] arraypass = new String[10];
        static int num=0;
        public static void main(String[] args) {
    
            while (true) {
                System.out.println("请选择功能:1.登录   2 注册  3.查看  4.退出" );
                int select = sc.nextInt();
                switch (select)
                {
                    case 1:
                        login();
                        break;
                    case 2:
                        register();
                        break;
                    case 3:
                        show_user();
                        break;
                    case 4:
                        return ;
                }
            }
        }
    
        public static void login()
        {
            System.out.println("欢迎使用登录功能");
            System.out.println("用户名:");
            String username = sc.next();
            System.out.println("密码:");
            String userpass = sc.next();
    
    //        与已有数据进行比对
            int status =0;
            for (int i = 0; i < arrayname.length; i++) {
                if (username.equals(arrayname[i]))
                {
                    if (userpass.equals(arraypass[i]))
                    {
                        System.out.println("登录成功");
                    }
                    else {
                        System.out.println("登录失败,账号或密码错误");
                    }
                    status++;
                }
            }
            if (status == 0)
            {
                System.out.println("未查询到该用户,请前往注册");
            }
    
        }
    
        public static void register()
        {
            System.out.println("欢迎使用注册功能");
            System.out.println("用户名:");
            String username = sc.next();
    
            int is_no =0;//判断是否存在 0不存在 大于0存在
            for (int i = 0; i < arrayname.length; i++) {
                if (username.equals(arrayname[i]))
                {
                    is_no++;
                }
            }
            if (is_no > 0)
            {
                System.out.println("该用户已经注册,请勿重复注册");
            }
            else
            {
                arrayname[num]  = username;
                System.out.println("密码:");
                String userpass = sc.next();
                arraypass[num] = userpass;
                num++;
                System.out.println("注册成功");
            }
    
        }
    
        public static void show_user()
        {
            if (num ==0)
            {
                System.out.println("暂无数据");
            }
            else{
                for (int i = 0; i < num; i++) {
                    System.out.print("用户名");
                    System.out.print(arrayname[i]);
                    System.out.print("密码");
                    System.out.println(arraypass[i]);
                }
            }
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
  • 相关阅读:
    RabbitMQ消息的可靠性
    【华为OD机试真题 python】 运维日志排序【2022 Q4 | 100分】
    【应用】Kubernetes
    『牛客|每日一题』N皇后问题
    深入总结MyBatis
    安装elasticsearch8.0.1之后无法访问9200Empty reply from server
    一文解读功率放大器(功率放大器如何选型)
    【App自动化测试】(十一)自动化关键数据记录
    [计算机提升] Windows系统权限
    【数据挖掘】2021年 Quiz 1-3 整理 带答案
  • 原文地址:https://blog.csdn.net/m0_73282576/article/details/132890435