0

我们的老师教我们关于if-else语句的知识,他正在教我们如何使用switch语句作为替代方案,if-else所以我在编码时遇到了这个无法访问的语句错误。我不知道我应该如何调试,因为我刚刚将我的 Java 从 6 更新到 8,而且我真的不知道我的代码有什么问题。

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;
                }

        }

    }
}
4

1 回答 1

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;
    }
于 2015-10-04T12:23:41.903 回答