用户注册登录v2

2020-05-17

1、近期学了构造方法,异常处理等,对之前的版本进行了修改,进行了方法的封装,增加了用户输入数据类型不匹配时的异常处理。

2、新知识点:main函数竟然可以递归,自己调用自己,长知识了。

3、期待学习窗口知识,做出窗口界面。

各个方法封装

package week9v2;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.util.Scanner;


public class Register_Login {

    //生成验证码
    public String SetRandom()
    {    //先将RandomNumber[0-61]赋值;把0-9,a-z,A-Z存取到数组中;
        char[] RandomNumber = new char [62];
        for(int i = 0; i < 62; i++) {
            if(i<10)
                RandomNumber[i] = (char)(i+48);
            else if (i<36) {
                RandomNumber[i] = (char)(i-10+65);
            }
            else {
                RandomNumber[i] = (char)(i-36+97);
            }
        }
        //随机获取四个RandomNumber[1-62]中的数生成验证码;
        String vilification = new String();
        for (int i = 0; i < 4; i++) {//随机获取四次
            vilification += RandomNumber[(int) (Math.random() * 62)];
    }
        return vilification;
    }


    Boolean  CheckRandom(String R1,String R2)//检查验证码是否正确
    {

        if (R2.equalsIgnoreCase(R1))//忽略大小写
            return true;
        else return false;
    }


    //注册界面
    public void Register ()throws Exception
    {

        Scanner input = new Scanner(System.in);//创建输入类
        System.out.println("注册一个新用户");
        System.out.print("请输入用户名: ");
        String username = input.next();
        System.out.print("请输入用户密码: ");
        String password = input.next();
        System.out.println(" 请输入验证码: ");
        Boolean pass = false;
        while(!pass) //若验证码不正确,则刷新验证码重输,直至正确
        {
            String vilification1=SetRandom();
            System.out.println(vilification1);//生成并输出验证码;

            String vilification2=input.next();//用户输入验证码;

            if (CheckRandom(vilification1, vilification2)) {
                System.out.println("注册成功!");
                File file = new File("users1.txt");
                //往文本中写入数据;
                FileOutputStream fileWrite=new FileOutputStream(file,true);//可追加内容
                java.io.PrintWriter output = new java.io.PrintWriter(fileWrite);
                //注册成功后才向文本里面写入用户名与密码;
                output.print(username +"\t");
                output.println(password);
                output.close();//输出完成后关闭文件
                pass = true;//跳出while循环;
            }
            else {
                System.out.println("验证码错误");

    }
            return;
    }
}


    //登录
    public void Login() throws FileNotFoundException{
            Scanner input = new Scanner(System.in);
            java.io.File file1 = new java.io.File("users1.txt");
            Scanner intput1 = new Scanner(file1);//从文件中读取信息

            //将数据读入数组
            String[] username = new String[100];
            String[] password = new String[100];
            int i=0;
            while (intput1.hasNext()) {//从文件中读取出名字密码,分别写进两个数组内,一一对应
                username[i] = intput1.next();
                password[i] = intput1.next();
                i++;
            }
            intput1.close();
            //输入用户名
            int correct = 0; //存放输入且正确的用户名下标
            int flag=0;
            System.out.print("请输入用户名: ");
            String username1 = input.next();
            for(int j = 0; j < username.length; j++)//i为总用户数,输入一个名字,与该数组中的元素一一匹配,查找是否存在
            {
                if(username1.equals(username[j]))
                {
                    System.out.print("请输入密码: ");
                    correct = j;//存放输入且正确的用户名下标
                    flag=1;
                    break;
                }
                else {
                    System.out.println("用户名不存在");
                    return;
                }
            }

           //输入密码
            if(flag==1)
            {
            Boolean pass = false;
            while (!pass) {//一直循环直到输入正确的密码,或者自己选择退出
                String password1 = input.next();
                if(password1.equals(password[correct]))//与它下标相对的密码
                {
                    System.out.println("登录成功");
                    pass = true;//跳出循环
                }
                else
                {
                    System.out.println("密码错误");
                    System.out.println("1-再次输入密码,2-退出登录界面,请选择:");
                    int select = input.nextInt();
                    if(select == 1) {
                        System.out.println("请输入密码: ");//继续返回while进行循环
                    }
                    else {

                        break;
                    }
                }

            }
        }
}
}
View Code

方法实现

package week9v2;

import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.Scanner;

public class TestLogin  {
    public static void main(String[] args) throws Exception {// 主函数也得加,否则调用错误
            Scanner input = new Scanner(System.in);
            Boolean close = false;
            while (!close) { // 完成跳转后返回初始的页面
                //String filename="user.txt";
                System.out.println("1-注册,2-登录,3-退出,请选择");
                try {
                    int Operation = input.nextInt();
                     Register_Login a=new  Register_Login();
                    switch (Operation) {
                    case 1:
                        a.Register();
                        break;
                    case 2:
                        a.Login();
                        break;
                    case 3:
                        System.exit(0);
                    default:
                        System.out.println("Error");
                        break;
                    }

    }

     catch (InputMismatchException ex) {
                    // TODO: handle exception,处理异常;

         System.out.println("输入数据类型有误!请重新输入:");
                 main(args);
     }
}
}
}

猜你喜欢

转载自www.cnblogs.com/sanens/p/12905244.html
v2