Java 使用开关的密码/用户名

Java 使用开关的密码/用户名,java,switch-statement,Java,Switch Statement,代码运行良好,但当我输入错误的用户名时,它只是终止,没有任何输出。关于这个代码有什么提示或建议吗?我知道如果。。。else,但我正在尝试使用开关来执行此操作 package frame.security; import java.util.Scanner; public class FrameIntel { public static void main(String[] args) { Scanner sc=new Scanner(System.in);

代码运行良好,但当我输入错误的用户名时,它只是终止,没有任何输出。关于这个代码有什么提示或建议吗?我知道如果。。。else,但我正在尝试使用
开关来执行此操作

package frame.security;
import java.util.Scanner;

public class FrameIntel {
    public static void main(String[] args) {

        Scanner sc=new Scanner(System.in);
        String Username;
        System.out.println("Enter Username:" );
        Username=sc.nextLine();
        String Password;
        System.out.println("Enter Password: ");
        Password=sc.nextLine();

        switch(Username) {
        case"Winford Coloma":
            if("TwelveEleven".equals(Password)) {
                System.out.println("Senior Software Engineer");
                System.err.println("Limited Access to Sandbox!(4)");
                break;
            }
            else {
                System.err.println("Access Denied!");
                System.exit(0);
            }
        case"Lynelle Marten":
            if("TwelveSixteen".equals(Password)) {
                System.out.println("Test Administrator");
                System.err.println("Limited Access to Sandbox!(5)");
                break;
            }
            else {
                System.err.println("Access Denied");
                System.exit(0);
            }
        case"Luis Ansley":
            if("TwelveTwenty".equals(Password)) {
                System.out.println("Software Engineer");
                System.err.println("Controlled Access to Sandbox!(Clog)");
                break;
            }
            else {
                System.err.println("Access Denied");
                System.exit(0);
            }
        case"Shantay Dority":
            if("TwelveTwentyFour".equals(Password)) {
                System.out.println("Programmer");
                System.err.println("Implement only(Sandbox)!");
                break;
            }
            else {
                System.err.println("Access Denied");
                System.exit(0);
            }
        case"Tangela Norsworthy":
            if("TwelveTwentySeven".equals(Password)){
                System.out.println("CEO");
                System.err.println("Full access to Sandbox!");
                break;
            }
            else {
                System.err.println("Access Denied");
                System.exit(0);
            } 


        }

    }
}

您没有
default
语句,因此您的代码将只与
case
语句中写入的用户名一起使用,您正在使用的switch语句没有defaultcase。 当没有“case”匹配时,使用默认case

在您的案例
“Tangela Norsworthy”
之后添加以下行:


System.exit();
”中的
0
表示应用程序已顺利退出。您应该使用其他值作为出现问题的指示。(即System.exit(1)-->1=无效用户名,System.exit(2)-->2=无效密码)

此处需要
默认值
default: {
    System.err.println("Username not recognised");
    System.exit(0);
}