Java 登录注册系统 【java基础案例教程 案例2-6】

案例要求

编写程序实现简单的登录注册系统。程序包括以下4个功能。

1)登录功能,用户输入正确的账号密码可成功登录

2)注册功能,输入用户名和密码进行注册

3)查看功能,查看所有的用户名和密码

4)退出功能,退出系统。

用户可以输入对应的编号进行相应的功能操作。例如输入"2"进行注册功能,输入用户名和密码进行注册。

本人按照自己的思路来写(水平有限)

首先注册再登录

1.我认为最难的点在于创建多个用户并且保存下来

解:运用二维数组进行保存

第一种定义方式:

数据类型[][] 数组名=new 数据类型[行的个数][列的个数]

第二种定义方式:

数据类型[][] 数组名={{},{},{},.....}

例 int[][] xx={{1,2},{3,2}}

   利用二维数组的特性  可以理解为数组中的数组  而里面的最里面数组就可以用来储存账号和密码

外面那一层的数组可以用来用户的定位。

   例 int[][] xx={{1,2},{3,2}}

   int[0][0]就是第1个用户的账号"1"  int[0][1]就是第1个用户的密码 "2"

   每进行注册操作int[0+1][0] 我们就可以换一行进行储存了(也就相当于新建了一个账户)

   这样我们就可以来储存多个用户和密码了。

    2.第二难点是 如何现实登录系统

    登录系统就要用户输入了

//先导包
import java.util.Scanner;
//新建Scanner实例
Scanner scan= new Scannner(System.in);
String inputAccount =scan.nextInt();

    账号与密码相匹配才显示登录成功 难就难在如何用户输入的账号密码与储存好的账号密码对比

    我们可以利用for循环遍历查找

    思路是 当边遍历边对比 

思路如下:

for(int i=0;i<用户二维数组的长度;i++){
      if(用户输入的账号.equals(数组用户二维数组[i][0])){//如是一样的我们就对比密码是否相同
        if(用户输入的密码.equals(数组用户二维数组[i][1])){
          System.out.println("登录成功");
         }
    }else{//如果没找账号或者是密码错误就 打印"登录失败"
     System.out.println("登录失败");
     }

}

这个是个思路 并不是最终代码



最大的问题我们解决完了 就可以开始写代码了!加上一些细节上的东西

这个系统需要用户的输入所以

//先导包
import java.util.Scanner;
//新建Scanner实例
Scanner scan= new Scannner(System.in);

我们先写后面我们要用到

我们可以先写界面

        System.out.println("===登录系统===");
        System.out.println("1.登录");
        System.out.println("2.注册");
        System.out.println("3.查看");
        System.out.println("4.退出");
        System.out.println("==注意== 未注册的用户请先注册");
        System.out.println("请输入编号进行操作");

实现输入对应的编号进行相应的功能操作的话 可以这么写:

System.out.println("===登录系统===");
        System.out.println("1.登录");
        System.out.println("2.注册");
        System.out.println("3.查看");
        System.out.println("4.退出");
        System.out.println("==注意== 未注册的用户请先注册");
        System.out.println("请输入编号进行操作");
        int input= scan.nextInt();
          if(input==1){
            account_1.login(numofregister);
          }else if(input==2){
              numofregister++;//每注册一个账户都会分配一个在String[][] allAccount 新位置
              account_1.register(numofregister);
          }else if(input==3){
              account_1.showInfo(numofregister);
              //退出功能
          }else if(input==4){
              System.out.println("是否要退出登录系统?Y/N");
              String quit=scan.next();
              if("Y".equals(quit)){
                  in=false;
              }else {
                  in=true;
              }
          }
 

numofregister是number of register 注册的数量 用这个变量名容易明白这个变量是用来干嘛的

代码段中的numofregister变量(全局变量)的作用是用来计数的 每进行注册操作 就自+1

else if(input==2){

     numofregister++;//每注册一个账户都会分配一个在String[][] allAccount 新位置
     account_1.register(numofregister);
}

就得写方法 写方法的同时我们可以建一个类 类名可以去为loginsystem

1.先加上成员变量 String[][] allAccount 用来储存用户数量和用户的账户和密码

class loinsystem{

//用户数量和用户的账户和密码存储
    String[][] allAccount=new String[100][2];




}

2.我们先写方法 第一个方法 注册方法

//注册方法
    public void register(int numofregister){
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入账号名");
        //String account用于接受用户输入的账号
        String account=scanner.next();
        System.out.println("请输入密码");
        //String password用于接受用户输入的密码
        String password=scanner.next();
        //储存用户的账号密码
        allAccount[numofregister][0]=account;
        allAccount[numofregister][1]=password;
        //提示注册成功
        System.out.println("注册成功!");
        System.out.println("\n");
    }

代码段中的numofregister变量的作用是计数 每进行注册操作就自+1 

像这段代码

//储存用户的账号密码
        allAccount[numofregister][0]=account;
        allAccount[numofregister][1]=password;

因为每进行注册操作 numofregister变量就自+1 也就是说换了个位置储存新账户和密码

3.登录方法

public void login(int numofregister){
        Scanner scan=new Scanner(System.in);
        System.out.println("请输入账号");
        //inputAccount用于接受用户的输入账号
        String inputAccount=scan.next();
        System.out.println("请输入密码");
        //inputPassword用于接受用户的输入密码
        String inputPassword= scan.next();
        //遍历查找用户的输入账号在数组的哪个位置以方便账号与密码匹配
        for(int i=0;i<allAccount.length;i++) {
            if (i <= numofregister) {//避免空指针异常
                if (inputAccount.equals(allAccount[i][0])) {//如果找到了账号在数组的位置就看密码与用户输入的密码是否匹配
                    if (inputPassword.equals(allAccount[i][1])) {
                        System.out.println("登录成功");
                    } else {
                        System.out.println("账号或密码有误 登录失败");
                    }
                }

            }else {//避免空指针异常
                break;
            }
        }
    }

为了避免空指针异常(因为没有在空闲的空间输入数据 这些空间就没有地址也就是null 空)

for(int i=0;i<allAccount.length;i++)

如果没有限制条件的话会一直循环下去 因为数组中可能有空闲的空间但是没有地址 当循环到这些空间时会抛出异常也就是空指针异常。

if (i <= numofregister){

}else{
break;
}

所以我们要"适可而止"  当循环到最后一位用户的位置时就结束循环 

3.查看方法

public void showInfo(int numofregister){
      for(int i=1;i<allAccount.length;i++){
          if(i<=numofregister){//避免空指针异常 避免遍历过度
              System.out.println("用户"+i+"信息如下:");
              System.out.println("账号:"+allAccount[i][0]);
              System.out.println("密码:"+allAccount[i][1]);
          }else {//避免空指针异常 避免遍历过度
              break;
          }

      }
    }

同理别循环过度了

加上一些细节完善一下

最终代码:

import java.util.Scanner;
public class LoginSystem {
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        boolean in=true;//用于开始和结束循环
        loinsystem account_1=new loinsystem();
        int numofregister=0;//用于计数
        //界面
        while(in){
        System.out.println("===登录系统===");
        System.out.println("1.登录");
        System.out.println("2.注册");
        System.out.println("3.查看");
        System.out.println("4.退出");
        System.out.println("==注意== 未注册的用户请先注册");
        System.out.println("请输入编号进行操作");
        int input= scan.nextInt();
          if(input==1){
            account_1.login(numofregister);
          }else if(input==2){
              numofregister++;//每注册一个账户都会分配一个在String[][] allAccount 新位置
              account_1.register(numofregister);
          }else if(input==3){
              account_1.showInfo(numofregister);
              //退出功能
          }else if(input==4){
              System.out.println("是否要退出登录系统?Y/N");
              String quit=scan.next();
              if("Y".equals(quit)){
                  in=false;
              }else {
                  in=true;
              }
          }
        }
    }
}
class loinsystem{
    //用户数量和用户的账户和密码存储
    String[][] allAccount=new String[100][2];

    //注册方法
    public void register(int numofregister){
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入账号名");
        //String account用于接受用户输入的账号
        String account=scanner.next();
        System.out.println("请输入密码");
        //String password用于接受用户输入的密码
        String password=scanner.next();
        //储存用户的账号密码
        allAccount[numofregister][0]=account;
        allAccount[numofregister][1]=password;
        //提示注册成功
        System.out.println("注册成功!");
        System.out.println("\n");
    }
    //登录方法
    public void login(int numofregister){
        Scanner scan=new Scanner(System.in);
        System.out.println("请输入账号");
        //inputAccount用于接受用户的输入账号
        String inputAccount=scan.next();
        System.out.println("请输入密码");
        //inputPassword用于接受用户的输入密码
        String inputPassword= scan.next();
        //遍历查找用户的输入账号在数组的哪个位置以方便账号与密码匹配
        for(int i=0;i<allAccount.length;i++) {
            if (i <= numofregister) {//避免空指针异常
                if (inputAccount.equals(allAccount[i][0])) {//如果找到了账号在数组的位置就看密码与用户输入的密码是否匹配
                    if (inputPassword.equals(allAccount[i][1])) {
                        System.out.println("登录成功");
                    } else {
                        System.out.println("账号或密码有误 登录失败");
                    }
                }

            }else {//避免空指针异常
                break;
            }
        }
    }
    //查看方法
    public void showInfo(int numofregister){
      for(int i=1;i<allAccount.length;i++){
          if(i<=numofregister){//避免空指针异常 避免遍历过度
              System.out.println("用户"+i+"信息如下:");
              System.out.println("账号:"+allAccount[i][0]);
              System.out.println("密码:"+allAccount[i][1]);
          }else {//避免空指针异常 避免遍历过度
              break;
          }

      }
    }
}

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值