我正在创建一个非常基本的登录系统来测试switch-case,但是我遇到了一个问题,即除非初始化变量,否则情况2无法运行。在我的程序案例1中创建了一个帐户,案例2正在登录该帐户。但是,案例2可以立即访问,但除非已创建帐户的详细信息,否则不会运行。我正在寻找一种拒绝访问案例2的方法,除非案例1首先完成。这可能吗?这是我的登录系统;
public class User {
private static Scanner in;
public static void main(String[] args) {
in = new Scanner(System.in);
int userChoice;
boolean quit = false;
do {
System.out.println("1. Create Account");
System.out.println("2. Login");
System.out.print("3. Quit");
userChoice = in.nextInt();
switch (userChoice) {
case 1:
String firstName;
String secondName;
String email;
String username;
String password;
System.out.print("Enter your first name: ");
firstName = in.nextLine();
System.out.println("Enter your second name:");
secondName = in.nextLine();
System.out.println("Enter your email address:");
email = in.nextLine();
System.out.println("Enter chosen username:");
username = in.nextLine();
System.out.println("Enter chosen password:");
password = in.nextLine();
break;
case 2:
String enteredUsername;
String enteredPassword;
System.out.print("Enter Username:");
enteredUsername = in.nextLine();
System.out.print("Enter Password:");
enteredPassword = in.nextLine();
if (enteredUsername == username && enteredPassword == password) {
System.out.println("Login Successfull!");
}
else
System.out.println("Login Failed!");
break;
case 3:
quit = true;
break;
default:
System.out.println("Wrong choice.");
break;
}
System.out.println();
} while (!quit);
System.out.println("Bye!");
}
}
我目前收到此错误;
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The local variable username may not have been initialized
The local variable password may not have been initialized
at User.main(User.java:68)
答案 0 :(得分:1)
你的范围有问题。
所以你有:
case 1:
String firstName;
String secondName;
String email;
String username;
String password;
问题是,案例2:在案例1中看不到用户名:因为它无法获得它。所以你应该在switch语句之前声明这些,这样你的代码就是这样的:
do {
System.out.println("1. Create Account");
System.out.println("2. Login");
System.out.print("3. Quit");
userChoice = in.nextInt();
String firstName ="";
String secondName ="";
String email ="";
String username ="";
String password ="";
switch (userChoice) {
case 1:
你会注意到我在字符串中添加了一个=“”,因为即使它们是空的,你也应该初始化它们。
字符串现在在switch语句之外声明,因此现在可以通过switch语句中的所有内容访问它们。
希望有所帮助。
答案 1 :(得分:1)
首先,您需要在while
循环之外声明您的帐户变量,否则每次while
循环运行时都会重新初始化它们。
其次,您可以先将变量手动初始化为null
,然后在案例2中检查。
最后,您混合了nextInt()
和nextLine()
的使用情况,这会导致扫描程序出现一些奇怪的UI问题。这是一个更正版本。
也不要使用==。
来比较Stringimport java.util.*;
public class User {
private static Scanner in;
public static void main(String[] args) {
in = new Scanner(System.in);
int userChoice;
boolean quit = false;
String firstName = null;
String secondName = null;
String email = null;
String username = null;
String password = null;
do {
System.out.println("1. Create Account");
System.out.println("2. Login");
System.out.println("3. Quit");
userChoice = Integer.parseInt(in.nextLine());
switch (userChoice) {
case 1:
System.out.print("Enter your first name: ");
firstName = in.nextLine();
System.out.println("Enter your second name:");
secondName = in.nextLine();
System.out.println("Enter your email address:");
email = in.nextLine();
System.out.println("Enter chosen username:");
username = in.nextLine();
System.out.println("Enter chosen password:");
password = in.nextLine();
break;
case 2:
String enteredUsername;
String enteredPassword;
System.out.print("Enter Username:");
enteredUsername = in.nextLine();
System.out.print("Enter Password:");
enteredPassword = in.nextLine();
if (username != null && password != null && enteredUsername.equals ( username) && enteredPassword.equals (password))
System.out.println("Login Successfull!");
else
System.out.println("Login Failed!");
break;
case 3:
quit = true;
break;
default:
System.out.println("Wrong choice.");
break;
}
System.out.println();
} while (!quit);
System.out.println("Bye!");
}
}
答案 2 :(得分:1)
正如编译器所说,你需要初始化一个局部变量,但主要的问题是你必须在switch块之外声明这些变量。并初始化它至少为null或""。
import java.util.Scanner;
public class User {
private static Scanner in;
public static void main(String[] args) {
in = new Scanner(System.in);
int userChoice;
boolean quit = false;
do {
System.out.println("1. Create Account");
System.out.println("2. Login");
System.out.print("3. Quit");
userChoice = in.nextInt();
String username = null; // MOVE HERE -------------
String password = null;
switch (userChoice) {
case 1:
String firstName;
String secondName;
String email;
System.out.print("Enter your first name: ");
firstName = in.nextLine();
System.out.println("Enter your second name:");
secondName = in.nextLine();
System.out.println("Enter your email address:");
email = in.nextLine();
System.out.println("Enter chosen username:");
username = in.nextLine();
System.out.println("Enter chosen password:");
password = in.nextLine();
break;
case 2:
String enteredUsername;
String enteredPassword;
System.out.print("Enter Username:");
enteredUsername = in.nextLine();
System.out.print("Enter Password:");
enteredPassword = in.nextLine();
if (enteredUsername == username && enteredPassword == password) {
System.out.println("Login Successfull!");
}
else
System.out.println("Login Failed!");
break;
case 3:
quit = true;
break;
default:
System.out.println("Wrong choice.");
break;
}
System.out.println();
} while (!quit);
System.out.println("Bye!");
}
}
答案 3 :(得分:1)
请尝试以下代码。在交换机外声明变量将起作用
import java.util.Scanner;
public class User {
private static Scanner in;
public static void main(String[] args) {
in = new Scanner(System.in);
int userChoice;
boolean quit = false;
String firstName = null;
String secondName = null;
String email = null;
String username = null;
String password = null;
String enteredUsername = null;
String enteredPassword = null;
do {
System.out.println("1. Create Account");
System.out.println("2. Login");
System.out.print("3. Quit");
userChoice = in.nextInt();
switch (userChoice) {
case 1:
System.out.print("Enter your first name: ");
do {
firstName = in.nextLine();
}while(firstName == null || firstName.equals(""));
System.out.println("Enter your second name:");
secondName = in.nextLine();
System.out.println("Enter your email address:");
email = in.nextLine();
System.out.println("Enter chosen username:");
username = in.nextLine();
System.out.println("Enter chosen password:");
password = in.nextLine();
break;
case 2:
System.out.print("Enter Username:");
do {
enteredUsername = in.nextLine();
}while(enteredUsername == null || enteredUsername.equals(""));
System.out.print("Enter Password:");
enteredPassword = in.nextLine();
if (enteredUsername.equals(username) && enteredPassword.equals(password)) {
System.out.println("Login Successfull!");
}
else
System.out.println("Login Failed!");
break;
case 3:
quit = true;
break;
default:
System.out.println("Wrong choice.");
break;
}
System.out.println();
} while (!quit);
System.out.println("Bye!");
}
}