目录
题目
编写程序实现简单的登录注册功能。程序包含以下4个功能:
(1)登录功能,用户输入正确的账号密码进行成功;
(2)注册功能,输入用户名和密码进行注册;
(3)查看功能,查看所有的用户名与密码;
(4)退出功能,退出系统。
用户可以输入对应的编号进行相应的功能操作。例如,输入2进入注册功能,输入用户名和密码进行注册。
代码
import java.util.Scanner;
public class Denglu{
public static String[] username = new String[3];
public static String[] password = new String[3];
static Scanner sc = new Scanner(System.in);
static int idx = 0;
public static void main(String[] args) {
username[0] = "heima";
password[0] = "123456";
Scanner sc = new Scanner(System.in);
boolean isFlag = true;
while (isFlag) {
print();
String choose = sc.next();
switch (choose) {
case "1":
login();
break;
case "2":
register();
break;
case "3":
show();
break;
case "0":
isFlag = false;
break;
default:
System.out.println("输入有误!请重新输入:!");
break;
}
}
sc.close();
}
public static void print() {
System.out.println("-----------黑马管理系统----------");
System.out.println("1.登录(login)");
System.out.println("2.注册(register)");
System.out.println("3.查看(show)");
System.out.println("0.退出(exit)");
System.out.println("-----------黑马管理系统----------");
System.out.println("请选择功能:");
}
public static void register() {
System.out.println("请输入用户名:");
String uname = sc.next();
System.out.println("请输入密码:");
String upass = sc.next();
boolean flag = true;
for (int i = 0; i < username.length; i++) {
if (uname.equals(username[i])) {
System.out.println("用户名已存在!");
flag = false;
}
}
if (flag) {
idx++;
if (idx < username.length) {
username[idx] = uname;
password[idx] = upass;
System.out.println("注册成功!\n");
} else {
add();
username[idx] = uname;
password[idx] = upass;
System.out.println("注册成功!");
}
}
}
public static void login(){
for(int i=0;i<3;i++) {
System.out.println("请输入用户名:");
String inputAccount=sc.next();
System.out.println("请输入密码:");
String inputPassword= sc.next();
for(int i1=0;i1<username.length;i1++) {
if (inputAccount.equals(username[idx])&&inputPassword.equals(password[idx])) {
System.out.println("登录成功!");
return;
}
}
System.out.println("账号或密码有误,登录失败!");
}
}
public static void add() {
String[] uarr = new String[username.length + 10];
String[] parr = new String[password.length + 10];
System.arraycopy(username, 0, uarr, 0, username.length);
System.arraycopy(password, 0, parr, 0, password.length);
username = uarr;
password = parr;
}
public static void show() {
for (int i = 0; i < username.length; ++i) {
if (username[i] != null) {
System.out.println("用户名:" + username[i] + ", 密码:" + password[i]);
}
}
}
}