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;

import javax.swing.plaf.basic.BasicInternalFrameTitlePane.IconifyAction;


public class Register_Loginv2 {

	//生成验证码
	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);//从文件中读取信息
			System.out.print("请输入用户名: ");
			String username1 = input.next();
			System.out.println("请输入密码:");
			String password = input.next();
			//将数据读入数组
			int i=0;
			int flag=0;
			while (intput1.hasNext())
			{
				String a=intput1.next();//用户名
				String b=intput1.next();//密码
				if (username1.equals(a))
				{
					if (password.equals(b)) {
						System.out.println("登录成功!");
						flag=1;
						break;
					}
				}

			}
			if (flag==0) {
				System.out.println("登录失败!");
	}
}
}

  

 

方法实现


package week9v2;


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


import week9v2.Register_Loginv2;


public class TestLogin2 {
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_Loginv2 a=new Register_Loginv2();

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);
}
}
}
}

 

 

posted on 2020-05-17 14:46  Sanen  阅读(203)  评论(0)    收藏  举报