如何解决:
// 问题1:(registe方法) 退出循环重新加载菜单会导致索引值清零,新数据覆盖旧数据,user[1]和user[2]为null
// 问题2:(login方法) if语句和else if语句从未被执行
(更新:用static定义数组的方式可以解决问题,不过我还想试试能不能在不使用static数组的情况下完成这个功能,参考大家的建议做了一些改动,结果又出现了新的问题
合并了choice和registe方法,问题1疑似解决了
问题2依旧存在
问题3:从菜单执行到login方法后会跳转去执行registe方法中的语句,可是我并没有在login方法中设置registe方法的入口,这是怎么回事呢?)
```java
public class question9 {
public static int cnt = 0;//注册次数
public static int index = 0;//数组索引
public static void main(String[] args) {
User[] user = new User[3];
registe(user);
}
public static void registe(User[] user) {
Scanner scanner = new Scanner(System.in);
System.out.println("1.注册 2.登陆 3.退出");
int choice = scanner.nextInt();
if (choice == 1) {
while (cnt != user.length) {
user[index] = new User(user);
System.out.println("用户名:");
user[index].setUsername(scanner.next());
System.out.println("密码:");
user[index].setPassword(scanner.next());
System.out.println("出生日期:");
user[index].setBirthdate(scanner.next());
System.out.println("爱好:");
user[index].setHobbies(scanner.next());
// continue;
cnt++;
index++;
registe(user);
}
//执行login方法后,无论用户名和密码如何设置,会跳转到这个位置继续执行
System.out.println("注册人数已满,禁止注册!");
registe(user);
} else if (choice == 2) {
login(user);
} else {
System.exit(0);
}
}
public static void login(User[] user) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名:");
String userlog = scanner.next();
System.out.println("请输入密码:");
String passlog = scanner.next();
// System.out.println(userlog + "..." + passlog);
//测试
//System.out.println(user[0]);//执行成功
//System.out.println(user[1]);//执行成功
//System.out.println(user[2]);//执行成功
//跳转到registe方法中choice==1 的 while循环的下面继续执行
//依旧执行失败
// 问题2:if语句和else if语句从未被执行
if (userlog.equals(user[0].getUsername()) && passlog.equals(user[0].getPassword())) {
user[0].toString();
} else if (userlog.equals(user[1].getUsername()) && passlog.equals(user[1].getPassword())) {
user[1].toString();
} else if (userlog.equals(user[2].getUsername()) && passlog.equals(user[2].getPassword())) {
user[2].toString();
} else {
System.out.println("用户名或密码错误!");// 只有本行能正常执行
}
// 测试
// String s1 = "1";
// System.out.println(s1.equals(userlog));
// System.out.println(s1.equals(user[0].getUsername()));
// System.out.println(userlog.equals(user[0].getUsername()));
// System.out.println(passlog.equals(user[0].getPassword()));
}
}
class User {
private String username;
private String password;
private String birthdate;
private String hobbies;
/**
*
*/
public User() {
super();
}
/**
* @param user
*/
public User(User[] user) {
// TODO Auto-generated constructor stub
}
/**
* @param username
* @param password
* @param birthdate
* @param hobbies
*/
public User(String username, String password, String birthdate, String hobbies) {
super();
this.username = username;
this.password = password;
this.birthdate = birthdate;
this.hobbies = hobbies;
}
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return the birthdate
*/
public String getBirthdate() {
return birthdate;
}
/**
* @param birthdate the birthdate to set
*/
public void setBirthdate(String birthdate) {
this.birthdate = birthdate;
}
/**
* @return the hobbies
*/
public String getHobbies() {
return hobbies;
}
/**
* @param hobbies the hobbies to set
*/
public void setHobbies(String hobbies) {
this.hobbies = hobbies;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + ", birthdate=" + birthdate + ", hobbies="
+ hobbies + "]";
}
}
```