本文实例为大家分享了java实现注册登录系统的具体代码,供大家参考,具体内容如下
1、创建菜单,注册,登录,退出
2、注册模块:
a) 通过键盘输入用户名,密码
b) 保存用户名密码到user.txt文件(包含用户名和密码)
c) 注册成功
3、登录模块
a) 通过键盘输入用户名和密码
b) 判断(超过三次提示过多错误,需要休眠30秒)
c) 登陆成功
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; import java.util.Scanner; class TestRegex{ public boolean isUser(String user) { String regex= "[1-9][0-9]{4,9}" ; boolean b=user.matches(regex); return b; } public boolean isMiMa(String mm) { String regex= "\\w+(\\.*\\w)" ; boolean b=mm.matches(regex); return b; } } public class MySQLregisterTest{ //1. 注册登录系统 //1. 创建菜单,注册,登录,退出 public static void MySQLmenu() { System.out.println( "***************************" ); System.out.println( "*****MySQL注册登录系统*****" ); System.out.println( "**1.注册" ); System.out.println( "**2.登录" ); System.out.println( "**3.退出" ); } //2. 注册模块: //a) 通过键盘输入用户名,密码 //b) 保存用户名密码到user.txt文件(包含用户名和密码) //c) 注册成功 public static void MySQLregister() throws IOException { TestRegex tr= new TestRegex(); File f= new File( "user.txt" ); Scanner sc= new Scanner(System.in); System.out.println( "欢迎来到注册界面!" ); System.out.println( "请输入用户名!" ); String s=sc.next(); boolean bu=tr.isUser(s); FileInputStream fis= new FileInputStream( "user.txt" ); Properties pro= new Properties(); pro.load(fis); String user=pro.getProperty( "user" ); String pass=pro.getProperty( "pass" ); if (bu== false &&user.equals(s)) { System.out.println( "账号注册失败" ); } else { FileOutputStream fos= new FileOutputStream(f, true ); byte [] bye= new byte [ 512 ]; int len= 0 ; fos.write(( "user=" +s+ "\r\n" ).getBytes()); fos.flush(); fos.close(); fis.close(); System.out.println( "注册成功" ); } System.out.println( "请输入密码!" ); String st=sc.next(); boolean bm=tr.isMiMa(st); if (bm== false &&pass.equals(st)) { System.out.println( "密码注册失败" ); } else { FileOutputStream fos= new FileOutputStream(f, true ); byte [] bye= new byte [ 512 ]; int len= 0 ; fos.write(( "pass=" +st+ "\r\n" ).getBytes()); fos.flush(); fos.close(); fis.close(); System.out.println( "账号注册成功" ); } } //3. 登录模块 //a) 通过键盘输入用户名和密码 public static boolean Login() throws IOException{ boolean flag= false ; Scanner sc= new Scanner(System.in); System.out.println( "请输入用户名:" ); String s=sc.next(); FileInputStream fis= new FileInputStream( "user.txt" ); Properties pro= new Properties(); pro.load(fis); String user=pro.getProperty( "user" ); String pass=pro.getProperty( "pass" ); if (s.equals(user)) { System.out.println( "请输入密码:" ); } String ms=sc.next(); if (ms.equals(pass)) { System.out.println( "登录成功" ); flag= true ; } return flag; } //b) 判断(超过三次提示过多错误,需要休眠30秒) //c) 登陆成功 public static void Oder() { int n = 1 ; abc: while (n < 4 ) { try { boolean flag = Login(); if (flag == false ) { n++; } else { System.out.println( "账号或密码错误,请确认账号密码" ); n = 4 ; break abc; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) throws IOException, Exception { boolean flag= true ; while (flag) { MySQLmenu(); Scanner sc= new Scanner(System.in); System.out.println( "请输入选择项:" ); int n=sc.nextInt(); switch (n) { case 1 : MySQLregister(); break ; case 2 : Oder(); System.out.println( "输入次数达到上限,休眠30秒" ); Thread.sleep( 30000 ); break ; case 3 : System.out.println( "已退出系统" ); flag= false ; break ; default : System.out.println( "输入异常!请重新输入" ); } } } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_39052339/article/details/100015691