String/StringBuilder 类 用对象数组实现登录注册功能

一、需求说明:实现用户注册、登陆功能;

  1. 程序中使用一个长度为3的对象数组,存储用户的登录名和密码:

    例如如下格式:

           登录名    密码      生日           爱好

zhangsan

1111

1998-03-15

旅游,唱歌

lisi

2222

2002-05-20

吃饭,睡觉,打豆豆

wangwu

3333

2001-07-28

看书,看电影,旅游

    注册时,要判断数组是否已满,是:则禁止注册。

  1. 循环为用户列出菜单:

    1.注册     2.登陆     3.退出

    用户选择1,调用:registe()方法,执行注册流程;

    用户选择2,调用:login()方法,执行登陆流程;

    用户选择3,结束程序;

  1. 编写方法:registe(),执行注册流程:

   l  要求用户输入以下信息(信息无需校验):

                            用户名:

                            登录密码:

                            出生日期:

                            爱好:(多个爱好中间用逗号(中英文都可以)隔开)

    1. 编写方法:login(),执行登陆流程:

      l  请用户输入登录名、登陆密码;

      l  使用登录名和密码进行验证;

        通过:打印:这个用户的所有信息;

        未通过:打印:用户名或密码错误!

import java.util.Scanner;
/*
 * 关于返回值为void的功能函数,其后增加return语句的作用的解释如下:
 * 无返回值函数

没有返回值的return语句只能用在返回类型是void的函数中。
返回void的函数不要求非得有return语句,因为在这类函数的最后一句后面会隐含地执行return。
通常情况下,void函数如果想在它的中间位置提前退出,可以使用return语句。return的这种用法有点类似于我们用break语句退出循环。
一个返回类型是void的函数也能使用return语句的第二种形式,不过此时return语句的expression必须是另一个返回void的函数。
强行令void函数返回其他类型的表达式将产生编译错误。
摘自:
https://www.cnblogs.com/wuchanming/p/3895337.html
 * */


public class Task001 { public static User[] userArray = new User[3]; public static int index = 0; public static void main(String[] args) { //下标值 Scanner sc = new Scanner(System.in); while(true){ System.out.println("1.注册 2.登录 3.退出"); int input = sc.nextInt(); switch(input){ case 1: registe(); break; case 2: login(); break; case 3: System.out.println("谢谢使用!"); System.exit(0); break; default: break; } } } public static void registe(){ /* 例如如下格式: 登录名 密码 生日 爱好 zhangsan 1111 1998-03-15 旅游,唱歌 lisi 2222 2002-05-20 吃饭,睡觉,打豆豆 wangwu 3333 2001-07-28 看书,看电影,旅游 注册时,要判断数组是否已满,是:则禁止注册。*/ Scanner sc = new Scanner(System.in); if(index > userArray.length ){ System.out.println("数据库已满"); return ; } System.out.println("请输入用户名:"); String username = sc.next().trim(); System.out.println("请输入密码:"); String password = sc.next().trim(); System.out.println("请输入生日:"); String birthday = sc.next().trim(); System.out.println("请输入爱好,不同爱好用,隔开"); String hobby = sc.next().trim(); User user = new User(username,password,birthday,hobby); userArray[index] = user; index++; System.out.println("注册成功!!!"); } //登录方法 public static void login(){ Scanner sc = new Scanner(System.in); System.out.println("请输入用户名:"); String username = sc.next().trim(); System.out.println("请输入密码:"); String password = sc.next().trim(); for (int i = 0; i < userArray.length; i++) { if(username.equals(userArray[i].getUsername()) && password.equals(userArray[i].getPassword())){ System.out.println("登录成功,你的个人信息为:"); System.out.println("用户名为:"+userArray[i].getUsername()+"生日为"+userArray[i].getBirthday()+"爱好为:"+userArray[i].getHobby()); } } System.out.println("你输入的账号或者密码不正确!!!"); } }

user 用户类:

public class User {
    private String username;
    private String password;
    private String birthday;
    private String hobby;
    
    //全参/空参构造方法
    
    public User(){}
    public User(String username,String password,String birthday,String hobby){
        this.username = username;
        this.password = password;
        this.birthday = birthday;
        this.hobby = hobby;
    }
    
    public String getUsername(){
        return username;
    }
    public void setUsername(String username){
        this.username = username;
    }
    public String getPassword(){
        return password;
    }
    public void setPassword(String password){
        this.password = password;
    }
    public String getBirthday(){
        return birthday;
    }
    public void setBirthday(String birthday){
        this.birthday = birthday;
    }
    public String getHobby(){
        return hobby;
    }
    public void setHobby(String hobby){
        this.hobby = hobby;
    }
}
原文地址:https://www.cnblogs.com/YangGC/p/8462186.html