除非变量已初始化,否则如何拒绝 switch-case 运行

How to deny switch-case running unless variable initialized

我正在创建一个非常基本的登录系统来测试 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)

你的范围有问题。

所以你有:

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;

    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!");

}

}

请尝试以下代码。在 switch 外部声明变量将起作用

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!");

 }
}