assignmentGradeAchieved

assignmentGradeAchieved

在我的程序中,我有一些科目,每个科目可以有很多作业。我正在尝试查找“ assignmentGradeAchieved”不为空的每个作业的平均成绩。

在一个数组中,我存储了这些assignmentGradeAchieved。我想找到平均结果(将每个数字相加然后除以项目数),但是它没有显示正确的arraysize。当它应为2时显示为1。
这是我尝试查找arraysize的代码行:
    arraySize =(assignmentGradesAchieved.size());

//对于属于该主题的每个作业,如果AssignmentGradeAchieved不为null,则将AssignmentGradeAchieved添加到名为AssignmentGradesAchieved的数组列表中。
//如果一个主题有多个具有AssignmentGradeAchieved的作业,则应将多个以上AssignmentGradeAchieved添加到名为AssignmentGradesAchieved的畜牧学家中。因此,数组的大小应大于一。
//我的一个主题有两个分配都都带有AssignmentGradeAchieved的数组,因此数组大小应为2而不是1

    for(int i=0; i<subjects.size(); i++){ //for each subject in the list
        Subject subject = subjects.get(i);


        List<Assignment> assignments = subject.getAssignment(); //Get the list of assignments belonging to current subject

            for(int y=0; y<assignments.size(); y++ ) { //For each assignment in the list
                Assignment assignment = assignments.get(y);

                ArrayList<Double> assignmentGradesAchieved = new ArrayList<Double>();
                if(assignments.get(y).getAssignmentGradeAchieved()!=null) {
                    assignmentGradesAchieved.add(assignments.get(y).getAssignmentGradeAchieved());
                    arraySize = 0;
                    arraySize = (assignmentGradesAchieved.size());
                    for(int z=0; z<assignmentGradesAchieved.size(); z++) {
                        arraySize = 0;
                        allAssignments = allAssignments + assignmentGradesAchieved.get(z);

                        arraySize = (assignmentGradesAchieved.size());
                        averageAssignGrade = (allAssignments / arraySize);

                    }
                }
    }


    }


解决方案:

   for(int i=0; i<subjects.size(); i++){ //for each subject in the list
        Subject subject = subjects.get(i);

        List<Assignment> assignments = subject.getAssignment(); //Get the list of assignments belonging to current subject
        ArrayList<Double> assignmentGradesAchieved = new ArrayList<Double>();

            for(int y=0; y<assignments.size(); y++ ) { //For each assignment in the list
                Assignment assignment = assignments.get(y);
                allAssignments =0;

                if(assignments.get(y).getAssignmentGradeAchieved()!=null) {
                    assignmentGradesAchieved.add(assignments.get(y).getAssignmentGradeAchieved());

                for(int z=0; z<assignmentGradesAchieved.size(); z++) {

                    allAssignments = allAssignments + assignmentGradesAchieved.get(z);
                }
                }

    }
            arraySize = 0;
            arraySize = (assignmentGradesAchieved.size());
            averageAssignGrade = (allAssignments / arraySize);


    }

最佳答案

您的assignmentGradesAchieved声明位于for循环内,因此它总是被重置,因此您的变量将始终只有一个赋值等级。将您的声明置于循环之外

07-28 11:17