java 8 unreachable语句switch case语句

时间:2015-10-04 12:10:21

标签: java switch-statement unreachable-statement

我们的老师教我们switch语句,他教我们if-else语句如何成为import java.io.*; import java.util.Scanner; class Switch { public static void main(String[] args) { Scanner k = new Scanner(System.in); System.out.println("Whats your name?"); String name = k.nextLine(); System.out.println("Do you want to compute your grades?"); String yesNo = k.nextLine(); switch(yesNo) { case "yes": System.out.println("Input your attendance grade(10%)\n"); int attendance = k.nextInt(); System.out.println("Input your participation grade (10%)\n"); int participation = k.nextInt(); System.out.println("Input your quiz grade (10%)\n"); int quiz = k.nextInt(); System.out.println("Input your exam grade(40%)\n"); int exam = k.nextInt(); System.out.println("Input your lab grade (20%)\n"); int lab = k.nextInt(); double point10 = 0.10; double point20 = 0.20; double point40 = 0.40; double attendanceC = attendance*point10; double participationC = participation*point10; double quizC = quiz*point20; double examC = exam*point40; double labC = lab*point20; double yourGradeD = attendanceC+participationC+quizC+examC+labC; int yourGrade = (int)yourGradeD; break; default: System.out.println("Thank you "+name); break; switch(yourGrade) { case 99|98|97|96|95|94|93|92|91|90: System.out.println("testing"); break; } } } } 的替代方案,所以我编码了,我得到了这个无法接受的陈述错误。我不想如何调试因为我刚刚将Java从6更新到8,而且我真的不知道我的代码有什么问题。

{{1}}

1 个答案:

答案 0 :(得分:0)

这是正确的代码,你的内部开关确实无法访问,因为它没有包含在case内。

在这里,我把它放在你的“是”case中,这是最合乎逻辑的。

注意:您的主开关只有两个选项:“是”或“默认”。在这种情况下,我更喜欢使用简单的if ... else

  switch(yesNo)
    {
        case "yes":
        System.out.println("Input your attendance grade(10%)\n");
        int attendance = k.nextInt();
        System.out.println("Input your participation grade (10%)\n");
        int participation = k.nextInt();

        System.out.println("Input your quiz grade (10%)\n");
        int quiz = k.nextInt();

        System.out.println("Input your exam grade(40%)\n");
        int exam = k.nextInt();

        System.out.println("Input your lab grade (20%)\n");
        int lab = k.nextInt();

        double point10 = 0.10;
        double point20 = 0.20;
        double point40 = 0.40;

        double attendanceC = attendance*point10;
        double participationC = participation*point10;
        double quizC = quiz*point20;
        double examC = exam*point40;
        double labC = lab*point20;

        double yourGradeD = attendanceC+participationC+quizC+examC+labC;
        int yourGrade = (int)yourGradeD;

        if(yourGrade >= 90 && yourGrade <= 99)
        {
            System.out.println("testing");
        }
        break;


        default:
        System.out.println("Thank you "+name);
        break;
    }