做一个有登陆注册界面以及做十以内加法测验功能的java系统代码
时间: 2023-07-16 08:16:08 浏览: 50
以下是一个简单的Java系统代码,包含登陆注册界面和十以内加法测验功能:
```java
import java.util.Random;
import java.util.Scanner;
public class MathTestSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
String[] users = new String[10];
String[] passwords = new String[10];
int[] scores = new int[10];
int currentUser = -1;
while (true) {
System.out.println("Welcome to Math Test System!");
System.out.println("1. Register");
System.out.println("2. Login");
System.out.println("3. Quit");
int choice = scanner.nextInt();
if (choice == 1) {
System.out.println("Please enter your username:");
String username = scanner.next();
System.out.println("Please enter your password:");
String password = scanner.next();
boolean userExists = false;
for (int i = 0; i < users.length; i++) {
if (username.equals(users[i])) {
userExists = true;
break;
}
}
if (userExists) {
System.out.println("This username is already taken.");
} else {
for (int i = 0; i < users.length; i++) {
if (users[i] == null) {
users[i] = username;
passwords[i] = password;
scores[i] = 0;
System.out.println("Registration successful.");
break;
}
}
}
} else if (choice == 2) {
System.out.println("Please enter your username:");
String username = scanner.next();
System.out.println("Please enter your password:");
String password = scanner.next();
boolean userExists = false;
for (int i = 0; i < users.length; i++) {
if (username.equals(users[i]) && password.equals(passwords[i])) {
userExists = true;
currentUser = i;
break;
}
}
if (userExists) {
System.out.println("Login successful.");
while (true) {
int num1 = random.nextInt(10);
int num2 = random.nextInt(10);
System.out.println("What is the sum of " + num1 + " and " + num2 + "?");
int answer = scanner.nextInt();
if (answer == num1 + num2) {
System.out.println("Correct!");
scores[currentUser]++;
} else {
System.out.println("Incorrect!");
}
System.out.println("Your score is " + scores[currentUser] + ".");
System.out.println("Do you want to continue (y/n)?");
String choice2 = scanner.next();
if (choice2.equals("n")) {
break;
}
}
} else {
System.out.println("Invalid username or password.");
}
} else if (choice == 3) {
System.out.println("Goodbye!");
break;
} else {
System.out.println("Invalid choice.");
}
}
}
}
```
这个系统使用了一个简单的数组来存储用户信息,包括用户名、密码和分数。用户可以选择注册或登录,注册时需要输入用户名和密码,如果用户名已经被占用,则注册失败。登录时需要输入正确的用户名和密码,登录成功后用户可以进行十以内加法测验,每做对一题分数加一,最后显示得分并询问是否继续。退出系统时会显示一条退出信息。