我究竟做错了什么?试图接收exa1 - exa4输入,但在输出上计算给我0

时间:2017-11-13 20:06:23

标签: java

public class Student extends Person{
    public double exa1, exa2, exa3, exa4, midexa1, midexa2, midexa3, finexa, finGrade;
    public char finCourseGrd;

    public Student(String f2, String l2, String a2,
            String n2, int edad2, int e1, int e2, int e3, int e4) {
        super(f2, l2, a2, n2, edad2);
        exa1 = e1;
        exa2 = e2;
        exa3 = e3;
        exa4 = e4;
    }

    public void midGrades() {
        midexa1 = (20/100)*(exa1/50);
        midexa2 = (20/100)*(exa2/50);
        midexa3 = (20/100)*(exa3/50);
    }

    public void exafinGrade() {
        finexa = (40/100)*(exa4/100);
    }

    public void finalGrade() {
        finGrade = ( midexa1 + midexa2 + midexa3 + finexa ) * 100;
    }

    public void finalCourseGrade() {
        if (finGrade >= 85 && finGrade <= 100) {
            finCourseGrd = 'A';
        }
        else if (finGrade >= 78 && finGrade <= 84) {
            finCourseGrd = 'B';
        }
        else if (finGrade >= 65 && finGrade <= 77) {
            finCourseGrd = 'C';
        }
        else if (finGrade >= 40 && finGrade <= 64) {
            finCourseGrd = 'D';
        }
        else if (finGrade >= 0 && finGrade <= 39) {
            finCourseGrd = 'F';
        }
    }

    public String toString() {
        return super.toString() + "First midterm Grade: " + midexa1 + "\n" + "Second midterm Grade: " + midexa2 + "\n" 
        + "Third midterm Grade: " + midexa3 + "\n" + "Final term Grade: " + finexa + "\n" + "The students final course grade: " + finGrade + finCourseGrd; 
    }

}

我有一个测试类,一切运行正常,但我有问题显示成绩的最终结果

1 个答案:

答案 0 :(得分:1)

在计算成绩时,您有一些施法问题。 因为它们被声明为整数所以输出将为零。 例如 20/100 o / p = 0 但如果明确地施放它们那么 (浮动)20 /(浮动)100 o / p = 0.200000

所以试试这个:

public void midGrades() {
        midexa1 = ((double)20/(double)100)*(exa1/50);
        midexa2 = ((double)20/(double)100)*(exa2/50);
        midexa3 = ((double)20/(double)100)*(exa3/50);
    }

    public void exafinGrade() {
        finexa = ((double)40/(double)100)*(exa4/100);
    }