几乎已经用 Java 实现了这个登录系统,但是我在使用这个方法时遇到了问题:
public void Register() {
Scanner sc = new Scanner(System.in);
System.out.print("Register? (Y/ N)\n");
String N = sc.nextLine();
if ("N".equals(N)) {
Login();
} else {
String Y = sc.nextLine();
if ("Y".equals(Y)) {
System.out.print("Email address: ");
String string = sc.nextLine();
System.out.print("Password: ");
String string2 = sc.nextLine();
System.out.print("\n\n");
new Products().search();
}
}
}
在 if 部分输入“N”完美无缺,但在 else 部分工作之前需要输入“Y”两次(我明白为什么它不起作用)。
我知道它很简单,但是关于如何让它工作有什么线索吗?
感谢任何帮助...
最佳答案
这里
String Y = sc.nextLine();
您正在阅读另一行输入。您想要比较您已读取的相同 输入行,您将其存储在名为 N
的变量中。如果你给它起一个更好的名字,它会更清楚。
String line = sc.nextLine();
if ("N".equals(line)) {
Login();
} else if ("Y".equals(line)) {
System.out.print("Email address: ");
...
}
关于Java登录系统方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51244857/