输出一个我不想得到的条件,可能代码块没有得到正确的管理

2o7dmzc5  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(324)

java新手,对块很难处理,当输入sum=9、examgrade=105和average=105时,输出只能是“错误无效数字”。
现在输出为:
“错误无效数字”
100
100
不知怎的到了else状态,没能处理好,提前谢谢你的帮助:)

public static void main(String[] args) {    
    Scanner input = new Scanner(System.in);

    int examGrade = 0, sum;
    double average = 0;

    System.out.println("Please enter your Exam Grade");
    examGrade = input.nextInt();

    System.out.println("Please enter your homework average");
    average = input.nextDouble();

    System.out.println("Please enter number of exercise");
    sum = input.nextInt();

    if (sum > 8 || examGrade > 100 || average > 100) {
        System.out.println("Error invaild numbers");
    }

    if (sum <= 4) {
        System.out.println("Your Final grade is zero");
    }

    if (sum == 5 || sum == 6) {
        if (examGrade >= 55) {
            System.out.println(examGrade*0.8 + average*0.2);
        }
    }
    else {
        System.out.println(examGrade);
    }

    if (sum == 7 || sum == 8) {
        if(examGrade <= 54) {
            if(average >= 80) {
                System.out.println(examGrade*0.75 + average*0.25);
            }
            else if(average < 80) {
                System.out.println(examGrade*0.8 + average*0.2);
            }
        }
    }
    else {
        System.out.println(examGrade*0.7 + average*0.3);
    }
}
2w3kk1z5

2w3kk1z51#

如果您有“无效数字”的情况,您可能不希望执行所有其他检查。要做到这一点,您需要将它们全部打包到一个类似以下的else中:

if (sum > 8 || examGrade > 100 || average > 100 ) {
    System.out.println("Error invaild numbers");
} else {
    if (sum <= 4 ) {
        System.out.println("Your Final grade is zero");
    }
    // and so on with all the other ifs
} // finish the "not invalid" block

相关问题