用Java代码设计一个用户登陆注册界面,要求用户信息用一个文本文件保存,当该文件不存在,则提示用户注册,将用户的信息写入文件,如果文件存在则进行密码核对,如果密码正确则进入系统,该系统使用随机函数生成四则运算的出题界面,用户可以对出题给出答案程序能够自动判断对错,先期设定运算题型,数据大小,题目数量,最后根据批改给出测试成绩。
时间: 2024-03-20 13:42:52 浏览: 87
好的,我会根据您的要求为您提供Java代码,但请注意,由于涉及到文件存储和用户登录等操作,建议在本地运行而非在线运行。以下是代码实现:
```java
import java.io.*;
import java.util.*;
public class UserLogin {
private static Scanner scanner = new Scanner(System.in);
private static final String USER_INFO_FILE = "users.txt";
public static void main(String[] args) {
boolean loggedIn = false;
String username = null;
String password = null;
Map<String, String> users = readUsersFromFile();
while (!loggedIn) {
System.out.print("请输入用户名:");
username = scanner.nextLine();
if (!users.containsKey(username)) {
System.out.println("用户不存在,请先注册!");
users.put(username, "");
continue;
}
System.out.print("请输入密码:");
password = scanner.nextLine();
if (!password.equals(users.get(username))) {
System.out.println("密码错误,请重新输入!");
continue;
}
System.out.println("登录成功!");
loggedIn = true;
}
System.out.println("欢迎进入四则运算练习系统!");
int num = getNumberOfQuestions();
int range = getRangeOfNumbers();
int correctCount = 0;
for (int i = 1; i <= num; i++) {
String question = generateQuestion(range);
System.out.printf("第%d题:%s\n", i, question);
int answer = getAnswer();
if (answer == calculate(question)) {
System.out.println("回答正确!");
correctCount++;
} else {
System.out.println("回答错误!");
}
}
double score = (double) correctCount / num * 100;
System.out.printf("测试结束,您的得分为%.2f分!\n", score);
updateScoreToFile(username, score);
}
private static Map<String, String> readUsersFromFile() {
Map<String, String> users = new HashMap<>();
File file = new File(USER_INFO_FILE);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("欢迎使用本系统,请先注册!");
return users;
}
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
String[] tokens = line.split(",");
users.put(tokens[0], tokens[1]);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return users;
}
private static void updateScoreToFile(String username, double score) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(USER_INFO_FILE, true));
writer.write(username + "," + score);
writer.newLine();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static int getNumberOfQuestions() {
System.out.print("请输入题目数量:");
int num = scanner.nextInt();
scanner.nextLine();
return num;
}
private static int getRangeOfNumbers() {
System.out.print("请输入数据范围:");
int range = scanner.nextInt();
scanner.nextLine();
return range;
}
private static String generateQuestion(int range) {
Random random = new Random();
int a = random.nextInt(range) + 1;
int b = random.nextInt(range) + 1;
int operator = random.nextInt(4);
String op = "";
switch (operator) {
case 0:
op = "+";
break;
case 1:
op = "-";
break;
case 2:
op = "*";
break;
case 3:
op = "/";
break;
}
return String.format("%d %s %d", a, op, b);
}
private static int getAnswer() {
int answer = 0;
boolean validInput = false;
while (!validInput) {
System.out.print("请输入答案:");
try {
answer = scanner.nextInt();
scanner.nextLine();
validInput = true;
} catch (InputMismatchException e) {
System.out.println("无效输入,请重新输入!");
scanner.nextLine();
}
}
return answer;
}
private static int calculate(String question) {
String[] tokens = question.split(" ");
int a = Integer.parseInt(tokens[0]);
int b = Integer.parseInt(tokens[2]);
switch (tokens[1]) {
case "+":
return a + b;
case "-":
return a - b;
case "*":
return a * b;
case "/":
return a / b;
default:
return 0;
}
}
}
```
上述代码实现了用户登录和注册、四则运算出题以及答案判断等功能。请将以上代码保存为 `UserLogin.java` 文件并在本地编译运行。
阅读全文