java实现实现简单的登录注册功能,程序包含以下4个功能。 (1)登录功能,用户输入正确的账号密码可成功登录 (2)注册功能,输入用户名和密码进行注册 (3)查看功能,查看所有的用户名和密码
时间: 2023-05-27 17:04:58 浏览: 41
(4)退出功能,退出程序。
以下是java代码实现:
import java.util.Scanner;
public class LoginRegister {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] users = new String[10];
String[] passwords = new String[10];
int count = 0; // 记录用户数
while (true) {
System.out.println("请选择功能:\n1.登录\n2.注册\n3.查看\n4.退出");
int choice = input.nextInt();
switch (choice) {
case 1:
System.out.print("请输入用户名:");
String username = input.next();
System.out.print("请输入密码:");
String password = input.next();
boolean flag = false; // 标记是否找到匹配的用户
for (int i = 0; i < count; i++) {
if (users[i].equals(username) && passwords[i].equals(password)) {
System.out.println("登录成功!");
flag = true;
break;
}
}
if (!flag) {
System.out.println("用户名或密码错误,请重新输入。");
}
break;
case 2:
System.out.print("请输入用户名:");
username = input.next();
System.out.print("请输入密码:");
password = input.next();
users[count] = username;
passwords[count] = password;
count++;
System.out.println("注册成功!");
break;
case 3:
System.out.println("所有的用户名和密码如下:");
for (int i = 0; i < count; i++) {
System.out.println(users[i] + "\t" + passwords[i]);
}
break;
case 4:
System.out.println("程序已退出。");
System.exit(0); // 强制退出程序
default:
System.out.println("输入有误,请重新选择。");
break;
}
}
}
}
注意:此程序仅为演示用途,实际应用中需要对用户输入进行更严格的验证和安全保护。