根据需要创建尽可能多的对象

时间:2013-05-18 09:34:00

标签: java

为什么我的平均方法不应该按照应有的方式工作 我添加了新的对象,但现在我的平均标记不能正常工作

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

    }


}

}

我的学生班(第四季),我知道这不是你班上最好的名字,但我有理由感谢

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

} //班级的结尾

2 个答案:

答案 0 :(得分: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循环来创建实例。

答案 1 :(得分:2)

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

while(numberOfStudent>0){
   create each student
   numberOfStudent--;
}