无法访问的代码,使用 Java 中的 switch 语句

huangapple go评论74阅读模式
英文:

Unreachable code using switch statements in Java

问题

// 询问部门
private String setDepartment() {
    Scanner sc = new Scanner(System.in);
    int department;
    
    System.out.println("选择您的部门:\n1. 销售\n2. 会计\n3. 开发\n4. 无");
    department = sc.nextInt();
    
    while (true) {
        switch (department) {
            case 1:
                this.department = "销售";
                break;
            case 2:
                this.department = "会计";
                break;
            case 3:
                this.department = "开发";
                break;
            case 4:
                this.department = "";
                break;
            default:
                System.out.println("请选择有效选项 (1-4)");
                break;
        }
        sc.close();
    }
    return this.department; // 错误:无法访问的代码
}
英文:

So I'm building an email administration app and although it is working with if statements, I am trying to incorporate switch statements. I am prompting the user using a scanner to select which department they are from while building their email associated with it. Although when I am trying to return this.department, it is saying it is unreachable. I feel like I am missing something very obvious.

// Ask for the department
private String setDepartment() {
    Scanner sc = new Scanner(System.in);
    int department;
    
    System.out.println("Select your department:\n1. Sales\n2.Accounting\n3.Development\n4.N/A");
    department = sc.nextInt();
    
    while (true) {
        switch (department) {
            case 1:
                this.department = "Sales";
                break;
            case 2:
                this.department = "Accounting";
                break;
            case 3:
                this.department = "Development";
                break;
            case 4:
                this.department = "";
                break;
            default:
                System.out.println("Please choose a valid option (1-4)");
                break;
        }
        sc.close();
    }
    return this.department; <-- Error: Unreachable code
}

答案1

得分: 1

问题在于你的while()循环内部的条件它始终被设置为true因此,在while循环下面的代码永远不会被执行。尝试在while循环内部使用不同的条件,该条件在达到特定值时终止。否则,你需要将想要返回的任何内容放在while循环内部。

附加:另外,请将sc.close();放在while循环之外,否则你不能在第一个while循环迭代之后继续使用该资源。因此,关闭资源必须始终在最后进行。

解决方案1:

while(updatedTerminationCondition){//更改终止条件



//在这里放置你要返回的值,但这样循环只会运行一次
}

解决方案2:

while(true){




//在这里放置你要返回的值,但这样循环只会运行一次
}
英文:

problem is your condition inside the while() loop .It set always to true .So code below the while loop never gets executed .Try to have a different condition inside the while loop which terminates in a certain value.Otherwise you need place what ever you want to return inside the while loop.

Extra: Please also place your sc.close(); outside the while loop otherwise you can't use the resource after the first while loop iteration.so closing resource must be done always at the end.

Solution 1:

while(updatedTerminationCondition){//change the termination condition





}

Solution 2:

while(true){




//place your return value inside here but then loop will only run once  
}

答案2

得分: 0

问题在于你的switch语句位于一个永远不会退出的while(true)循环中。除非你明确地通过使用break语句退出循环,否则程序将永远不会停止执行该循环,这意味着其下方的任何代码都无法被执行到,这就是编译器报错的原因所在。

英文:

The problem is your switch-statement is in a while(true) loop that gets never exited. Unless you explicitly exit the loop e.g. by using break the program will never stop to execute the loop, meaning any code below it will never get reached, thats what the compiler is complaining about.

huangapple
  • 本文由 发表于 2020年10月19日 03:31:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64417498.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定