2014-12-04 80 views
-2

我很新的編程,所以我需要幫助。我想添加SubjectGrades到studentList ArrayList。但我認爲我做錯了。我應該怎麼做才能將SubjectGrades添加到ArrayList中?謝謝Java - 在ArrayList中添加2個對象

這是我的部分主類。

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

public class Main { 

private static Scanner in; 

public static void main(String[] args) { 

    ArrayList<Student> studentList = new ArrayList<Student>(); 
    //ArrayList<SubjectGrades> Grades = new ArrayList<SubjectGrades>(); 
    in = new Scanner(System.in); 
    String search, inSwitch1, inSwitch2; 
    int inp; 

    do { 
     SubjectGrades sGrade = new SubjectGrades(); 
     Student student = new Student(); 

     System.out.println("--------------------------------------"); 
     System.out.println("What do you want to do?"); 
     System.out.println("[1]Add Student"); 
     System.out.println("[2]Find Student"); 
     System.out.println("[3]Exit Program"); 
     System.out.println("--------------------------------------"); 

     inSwitch1 = in.next(); 

     switch (inSwitch1) { 

     case "1": 

      System.out.println("Input student's Last Name:"); 
      student.setLastName(in.next()); 
      System.out.println("Input student's First Name:"); 
      student.setFirstName(in.next()); 
      System.out.println("Input student's course:"); 
      student.setCourse(in.next()); 
      System.out.println("Input student's birthday(mm/dd/yyyy)"); 
      student.setBirthday(in.next()); 
      System.out.println("Input Math grade:"); 
      student.subjectGrade.setMathGrade(in.nextDouble()); 
      System.out.println("Input English grade:"); 
      student.subjectGrade.setEnglishGrade(in.nextDouble()); 
      System.out.println("Input Filipino grade:"); 
      student.subjectGrade.setFilipinoGrade(in.nextDouble()); 
      System.out.println("Input Java grade:"); 
      student.subjectGrade.setJavaGrade(in.nextDouble()); 
      System.out.println("Input SoftEng grade:"); 
      student.subjectGrade.setSoftEngGrade(in.nextDouble()); 
      studentList.add(student); 
      studentList.add(student.setSubjectGrade(sGrade)); //Here it is that I want to add 
      break; 
      //end case 1 

這是我的學生課。

package santiago; 

public class Student { 

private String lastName; 
private String firstName; 
private String course; 
private String birthday; 

SubjectGrades subjectGrade = new SubjectGrades(); 



public SubjectGrades getSubjectGrade() { 
    return subjectGrade; 
} 

public void setSubjectGrade(SubjectGrades subjectGrade) { 
    this.subjectGrade = subjectGrade; 
} 

public String getLastName() { 
    return lastName; 
} 

public void setLastName(String lastName) { 
    this.lastName = lastName; 
} 

public String getFirstName() { 
    return firstName; 
} 

public void setFirstName(String firstName) { 
    this.firstName = firstName; 
} 

public String getCourse() { 
    return course; 
} 

public void setCourse(String course) { 
    this.course = course; 
} 

public String getBirthday() { 
    return birthday; 
} 

public void setBirthday(String birthday) { 
    this.birthday = birthday; 
} 

} 

而且我SubjectGrades類

package santiago; 

public class SubjectGrades{ 


Double mathGrade, englishGrade, filipinoGrade, javaGrade, softEngGrade, weightedAverage; 

public Double getMathGrade() { 
    return mathGrade; 
} 

public void setMathGrade(Double mathGrade) { 
    this.mathGrade = mathGrade; 
} 

public Double getEnglishGrade() { 
    return englishGrade; 
} 

public void setEnglishGrade(Double englishGrade) { 
    this.englishGrade = englishGrade; 
} 

public Double getFilipinoGrade() { 
    return filipinoGrade; 
} 

public void setFilipinoGrade(Double filipinoGrade) { 
    this.filipinoGrade = filipinoGrade; 
} 

public Double getJavaGrade() { 
    return javaGrade; 
} 

public void setJavaGrade(Double javaGrade) { 
    this.javaGrade = javaGrade; 
} 

public Double getSoftEngGrade() { 
    return softEngGrade; 
} 

public void setSoftEngGrade(Double softEngGrade) { 
    this.softEngGrade = softEngGrade; 
} 

public Double getWeightedAverage(){ 
    weightedAverage = ((mathGrade + englishGrade + filipinoGrade + javaGrade + softEngGrade)*3)/15; 

    return weightedAverage; 
} 

public String getScholarStatus(){ 
    String status = ""; 

    if(weightedAverage <= 1.5) { 
     status = "full-scholar"; 

    } else if (weightedAverage <= 1.75){ 
     status = "half-scholar" ; 
    } else { 
     status = "not a scholar"; 

    } 

    return status; 
} 
} 
+0

訪問它不能類型'SubjectGrades'的對象添加到一個ArrayList的''。 – 2014-12-04 12:29:57

+0

他們要求我只有一個ArrayList。 – Zelgh 2014-12-04 12:31:46

回答

0

你的錯誤:

studentList.add(student); 
studentList.add(student.setSubjectGrade(sGrade)); 

您要添加的學生,然後嘗試添加void。的setSubjectGrade返回值是void,所以什麼也不會說: 只是做:

student.setSubjectGrade(sGrade); 
studentList.add(student); 

哪裏sGradeSubjectGrades類型,這是在以同樣的方式 student.subjectGrade.setSoftEngGrade(in.nextDouble());被填充填充的Object

+1

如果你這樣做,你應該改變你設置成績值的所有地方。目前,這些值被賦予'student'的'subjectGrade'屬性,而不是'sGrade'。 – James 2014-12-04 12:38:20

+0

它工作。謝謝。 – Zelgh 2014-12-04 12:43:22

+0

我假設你創建了'sGrade',就像上面建議的註釋:) – Ian2thedv 2014-12-04 12:45:48

0

使用

ArrayList <SubjectGrades> list; 

學生類,而不是SubjectGrades subjectGrade = new SubjectGrades();

和生成getter和setter

0

只是刪除這一行:

studentList.add(student.setSubjectGrade(sGrade)); //Here it is that I want to add 

你做了它的方式,學生對象已經與設置其值subjectGrade屬性。

您可以studentList.get(0).getSubjectGrade()

+0

這也幫助了我,謝謝。 – Zelgh 2014-12-04 12:43:38