Java 如何在变量未初始化之前拒绝开关案例的运行
我正在创建一个非常基本的登录系统来测试switch案例,但是我遇到了一个问题,案例2无法运行,除非初始化一个变量。在我的程序中,案例1正在创建一个帐户,案例2正在登录该帐户。但是,案例2可以直接访问,但除非已创建帐户的详细信息,否则不会运行。我正在寻找一种方法来拒绝访问案例2,除非案例1已经首先完成。这可能吗?这是我目前的登录系统Java 如何在变量未初始化之前拒绝开关案例的运行,java,variables,switch-statement,Java,Variables,Switch Statement,我正在创建一个非常基本的登录系统来测试switch案例,但是我遇到了一个问题,案例2无法运行,除非初始化一个变量。在我的程序中,案例1正在创建一个帐户,案例2正在登录该帐户。但是,案例2可以直接访问,但除非已创建帐户的详细信息,否则不会运行。我正在寻找一种方法来拒绝访问案例2,除非案例1已经首先完成。这可能吗?这是我目前的登录系统 public class User { private static Scanner in; public static void main(String[] a
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)
你对范围有问题 所以你有:
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语句中的所有内容现在都可以访问它们
希望这会有所帮助。首先,您需要在
while
循环之外声明帐户变量,否则每次while
循环运行时都会重新初始化帐户变量
其次,您可以先手动将变量初始化为null
,然后在案例2中进行检查
最后,您混合使用了nextInt()
和nextLine()
,这将导致扫描仪出现一些奇怪的UI问题。这是一个正确的版本
也不要使用==比较字符串
import 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!");
}
}
正如编译器所说,您需要初始化一个局部变量,但主要问题是您必须在开关块之外声明这些变量。并至少将其初始化为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;
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!");
}
}
如果用户先键入2,会发生什么情况?可以尝试登录,但用户没有登录凭据。要做的第一件事:在代码编译之前不要尝试运行代码!这个密码把我弄糊涂了。。。为什么不把它改成if…else if。。。如果已创建帐户,则使用布尔控制语句。记录“案例1”的变量已运行,如果未运行,则使用
if
语句阻止“案例2”?这真的需要成为堆栈溢出问题吗?最后,不要将用户名和密码与==,而是与enteredusername.equals(用户名)和输入的password.equals(密码)进行比较,否则我很快就会看到另一个堆栈溢出问题!
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!");
}
}