問(wèn)一個(gè)小問(wèn)題,我for循環(huán)那里出現(xiàn)了null pointer我檢查了幾遍都沒(méi)出問(wèn)題
package mxxx;
public class duoduiduo {
public static void main(String[] args) {
// TODO Auto-generated method stub
? ?
? //學(xué)生信息
? ? ? ? ? ? ? ?Student stu1=new Student(1,"張小跳",18);
? ? ? ? ? ? ? ?Student stu2=new Student(2,"倩倩",19);
? ? ? ? ? ? ? ?Student stu3=new Student(3,"小洛",15);
? ? ? ? ? ? ? ?//課程信息
? ? ? ? ? ? ? ?Course ca=new Course(1122,"政治思想",3);
? ? ? ? ? ? ? ?Course cb=new Course(1125,"高等數(shù)學(xué)",4); ?
? ? ? ? ? ? ? ?//stu1和stu3選了課程ca分別的78和87分
? ? ? ? ? ? ? ?ca.setStudentCourse(new StudentCourse[]{new StudentCourse(stu1,ca,78),new StudentCourse(stu3,ca,87)});
? ? ? ? ? ? ? ?//stu2和stu3選了課程cb,分別的84和68分
? ? ? ? ? ? ? ?cb.setStudentCourse(new StudentCourse[]{new StudentCourse(stu2,cb,84),new StudentCourse(stu3,cb,68)});
? ? ? ? ? ? ? ?//stu1選了ca課程得78分
? ? ? ? ? ? ? ?stu1.setStudentCourse(new StudentCourse[]{new StudentCourse(stu1,ca,78)});
? ? ? ? ? ? ? ?//stu2選了cb課程得84分
? ? ? ? ? ? ? ?stu2.setStudentCourse(new StudentCourse[]{new StudentCourse(stu2,cb,84)});
? ? ? ? ? ? ? ?//stu3選了ca和cb課程分別得87和68分;
? ? ? ? ? ? ? ?stu3.setStudentCourse(new StudentCourse[]{new StudentCourse(stu3,ca,87),new StudentCourse(stu3,cb,68)});
? ? ? ? ? ? ? //課程信息
? ? ? ? ? System.out.println(stu3.getstudentInfo()); ? //輸出學(xué)生1得信息
? ? ? ? ? for(int i=0;i<stu3.getStudentCourse().length;i++){
? ? ? ? ?System.out.print(stu3.getStudentCourse()[i].getCourse().getCourseInfo());
? ? ? ? ?System.out.println(",成績(jī)"+stu3.getStudentCourse()[i].getScore());
? ? ? ? ? }
}
}
class Student{
private int stuId;
private String stuName;
private int age;
private StudentCourse studentCourse []; ?//這個(gè)里面包括學(xué)生信息和課程信息,所以學(xué)生和課程不需要互相自定義類型
public Student (int stuId,String stuName,int age){
this.stuId=stuId;
this.stuName=stuName;
this.age=age;
}
public void setStudentCourse(StudentCourse studentCouse[]){
this.studentCourse=studentCourse;
}
public StudentCourse[] getStudentCourse(){
return this.studentCourse;
}
public String getstudentInfo(){
return "學(xué)號(hào):"+stuId+",姓名:"+stuName+",年齡:"+age;
}
}
class Course{
private int cid; //課程編號(hào)
private String cName;
private int credit; ?//學(xué)分
private StudentCourse studentCourse [];
public Course(int cid,String cName,int credit){
this.cid=cid;
this.cName=cName;
this.credit=credit;
}
public void setStudentCourse(StudentCourse studentCouse[]){
this.studentCourse=studentCourse;
}
public StudentCourse[] getStudentCourse(){
return this.studentCourse;
}
public String getCourseInfo(){
return "課程編號(hào):"+cid+",課程名稱:"+cName+",課程學(xué)分:"+credit;
}
}
class StudentCourse{
private Student student;//學(xué)生信息
private Course course; ?//課程信息
private double score; ? //成績(jī)
public StudentCourse(){};
public StudentCourse(Student student,Course course,double score){//這個(gè)為student和course賦值,所以這個(gè)不需要set方法
this.student=student;
this.score=score;
this.course=course;
}
public String getStudentCourseInfo(){
return "學(xué)生信息"+student+",課程信息"+course+",成績(jī):"+score;
}
//下面這個(gè)我認(rèn)為這個(gè)是點(diǎn)睛之筆
public Student getStudent(){
return this.student;
}
public Course getCourse(){
return this.course;
}
? public double getScore(){
?return this.score;
? }
}
2018-08-08
public void setStudentCourse(StudentCourse studentCouse[]){
this.studentCourse=studentCourse;
}
就這段,兄嘚,你的方法中的參數(shù)數(shù)據(jù)studentCourse少了個(gè)r
2018-03-20
看了半天,沒(méi)看出問(wèn)題