-1

为什么我的平均方法不能正常工作,应该是我让添加新对象工作但现在我的平均分数不能正常工作

  import java.util.ArrayList;
  import java.util.Scanner;

  public class Q4Test {

public static void main(String[] args) {

    int[] markArr = new int[1];
    int[] creditArr = new int[1];

    Scanner scn = new Scanner(System.in);

    System.out.print("Number of Student :");
    int numberOfStudent = scn.nextInt();


    ArrayList<Q4> myStudentList = new ArrayList<Q4>();


    for (int i = 0; i < numberOfStudent; i++) {

        System.out.println();
        System.out.print("Student " + (i + 1) + " Name  :");
        String stName = scn.next();

        System.out.print("Student ID :");
        int stID = scn.nextInt();

        System.out.print("Number of Units :");
        int stNumOfUnit = scn.nextInt();

        for (int x = 0; x < stNumOfUnit; x++) {

            markArr = new int[stNumOfUnit];

            System.out.print("Mark for Unit " + (x + 1) + " :");
            int mark = scn.nextInt();

            markArr[x] = mark;

        }

        for (int x = 0; x < stNumOfUnit; x++) {

            System.out.print("Credit for Unit " + (x + 1) + " :");
            int credit = scn.nextInt();

            creditArr = new int[stNumOfUnit];
            creditArr[x] = credit;

        }

        myStudentList.add(new Q4(stName, stID, stNumOfUnit, markArr, creditArr));
        myStudentList.get(i).averageMark(markArr);

        System.out.println(myStudentList.get(i).averageMark(markArr));

    }


}

}

我的学生班(Q4),我知道这不是你班最好的名字,但我有我的理由谢谢

 import java.io.Serializable;
  import java.util.*;

 public class Q4 implements Serializable {

private String studentName;
private int studentID;
private  int numberOFUnit;
private int[] mark;
private int[] credit;

public Q4(){

}

public Q4(String studentName,int studentID,int numberOFUnit,int[] mark,int[] credit){

    this.studentID = studentID;
    this.studentID = studentID;
    this.numberOFUnit = numberOFUnit;
    this.mark = mark;
    this.credit = credit;

}// end of constructor

public String getStudentName() {
    return studentName;
}

public int getStudentID() {
    return studentID;
}

public int getNumberOFUnit() {
    return numberOFUnit;
}

public int[] getCredit() {
    return credit;
}

public int[] getMark() {
    return mark;
}

public void setStudentName(String studentName) {

    this.studentName = studentName;
}

public void setStudentID(int studentID) {
    this.studentID = studentID;
}

public void setNumberOFUnit(int numberOFUnit) {
    this.numberOFUnit = numberOFUnit;
}

public void setMark(int[] mark) {
    this.mark = mark;
}

public void setCredit(int[] credit) {
    this.credit = credit;
}

public double averageMark(int[] array){

    double sum = 0;

    for(int i=0 ; i < array.length ; i++){

        sum = sum + array[i];

    }

    return sum/array.length;
}

}//课程结束

4

2 回答 2

5
System.out.print("Number of Student :");
int numberOfStudent = scn.nextInt();
ArrayList<Q4> myStudentList = new ArrayList<Q4>();
for (int i = 0; i < numberOfStudent; i++) {
     myStudentList.add(new Q4()); 
}

尽管您已经实现了您在问题中所说的内容,但这将允许您通过使用 arraylist 和 for 循环来创建实例。

于 2013-05-18T09:39:46.787 回答
2

选中此项以创建您想要的学生

while(numberOfStudent>0){
   create each student
   numberOfStudent--;
}
于 2013-05-18T09:40:36.743 回答