1.题目具体要求如下: /* 程序实现注册、登录功能。用户名、密码存储在user_g.txt文件中。 注册时,要验证“用户名”是否存在,如果存在,则注册失败。 注册成功后,用户可以使用已注册的用户名和密码进行登录。 登录后提示:欢迎xxxx登录系统,然后用户登录后,可以玩“猜数字”小游戏。 如果一个用户玩过三次,当第四次启动时,提示:试玩三次结束,请续费!! (提示:用户无论登录多少次,只要玩过三次,就不允许再玩了) 如果登录三次失败,程序不允许此用户继续登录了! (说明:如果三次登录失败,即使重启程序,也不允许此用户登录了) */
2.代码实现如下:
import java.io.*;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
while (true) {
System.out.println("******欢迎来到猜数字系统******\r\n");
System.out.println("1.登录\t\t2.注册\t\t3.退出\r\n");
System.out.println("****************************");
System.out.println("请输入你要操作的序号:");
try {
int i = new Scanner(System.in).nextInt();
switch (i) {
case 1:
logon();
break;
case 2:
login();
break;
case 3:
System.exit(0);
default:
System.out.println("你输入的编号有误!");
}
} catch (InputMismatchException e) {
System.out.println("请输入序号!亲");
}
}
}
private static void logon() {
Scanner sc = new Scanner(System.in);
ArrayList<String> list = readFile();
boolean b = list.isEmpty();
if (b){
System.out.println("当前系统没有用户!");
return;
}
while (!b){
System.out.println("请输入用户名:");
String name = sc.next();
String s1 = null;
for (String s : list) {
if (s.startsWith(name)){
s1 = s;
}
}
if (!list.contains(s1)){
System.out.print("用户名不存在,重新");
} else {
while (true) {
String[] split = s1.split(",");
if (Integer.parseInt(split[2])>0) {
System.out.println("请输入密码:");
String psw = sc.next();
if (psw.equals(split[1])) {
System.out.println("密码正确,登录成功!");
int i = guess(Integer.parseInt(split[3]));
list.remove(s1);
list.add(split[0]+","+split[1]+","+3+","+i);
writeFile(list);
return;
} else {
pswFalse(split[0]);
ArrayList<String> list1 = readFile();
String s2 = null;
for (String s : list1) {
if (s.startsWith(name)) {
s2 = s;
}
}
String[] split1 = s2.split(",");
if (Integer.parseInt(split1[2]) > 0) {
System.out.print("密码错误,你还有" + split1[2] + "次机会,重新");
} else {
System.out.println("密码错误三次!已锁定,请联系管理员解锁。");
break;
}
}
}else {
System.out.println("您的帐号已锁定,请联系管理员解锁。");
return;
}
}
}
}
}
private static void login() {
ArrayList<String> list = readFile();
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入用户名:");
String name = sc.next();
String s1 = null;
for (String s : list) {
if (s.startsWith(name)){
s1 = s;
}
}
if (list.contains(s1)){
System.out.print("用户名已存在,重新");
}else {
System.out.println("请输入密码:");
String psw = sc.next();
list.add(name + "," + psw + ",3,3");
writeFile(list);
System.out.println("注册成功!");
break;
}
}
}
private static ArrayList<String> readFile(){
ArrayList<String> list = new ArrayList<>();
try (BufferedReader in = new BufferedReader(new FileReader("user_g.txt"))) {
String row = null;
while ((row = in.readLine()) != null){
list.add(row);
}
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
private static void writeFile(ArrayList<String> list){
try (BufferedWriter out = new BufferedWriter(new FileWriter("user_g.txt"))) {
for (String s : list) {
out.write(s);
out.newLine();
}
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static int guess(int index) {
System.out.println("欢迎来玩猜字小游戏");
if (index > 0) {
boolean b = true;
while (b) {
int number = new Random().nextInt(100) + 1;
System.out.println("请输入一个1到100之间的数字:");
index--;
while (true) {
int i = new Scanner(System.in).nextInt();
if (i == number) {
System.out.println("恭喜你猜对了!");
if (index >= 1) {
System.out.println("你还可以试玩" + index + "次。");
System.out.println("是否继续试玩?(是输入1,否输入0)");
int s = new Scanner(System.in).nextInt();
if (s == 0) {
b = false;
}
} else {
b = false;
System.out.println("试玩结束,请付费!");
}
break;
} else if (i > number) {
System.out.println("你猜大了,再猜!");
} else if (i < number) {
System.out.println("你猜小了,再猜!");
}
}
}
}else {
System.out.println("您的三次试玩已用完,请付费后再玩!");
}
return index;
}
private static void pswFalse(String n){
ArrayList<String> list = readFile();
String s1 = null;
for (String s : list) {
if (s.startsWith(n)){
s1 = s;
}
}
String[] split = s1.split(",");
if (list.contains(s1)){
int i = Integer.parseInt(split[2]);
i = i-1;
list.remove(s1);
list.add(split[0]+","+split[1]+","+i+","+split[3]);
writeFile(list);
}
}
}