亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

問一個小問題,我for循環那里出現了null pointer我檢查了幾遍都沒出問題

package mxxx;


public class duoduiduo {


public static void main(String[] args) {

// TODO Auto-generated method stub

? ?

? //學生信息

? ? ? ? ? ? ? ?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,"高等數學",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()); ? //輸出學生1得信息

? ? ? ? ? for(int i=0;i<stu3.getStudentCourse().length;i++){

? ? ? ? ?System.out.print(stu3.getStudentCourse()[i].getCourse().getCourseInfo());

? ? ? ? ?System.out.println(",成績"+stu3.getStudentCourse()[i].getScore());

? ? ? ? ? }

}


}

class Student{

private int stuId;

private String stuName;

private int age;

private StudentCourse studentCourse []; ?//這個里面包括學生信息和課程信息,所以學生和課程不需要互相自定義類型

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 "學號:"+stuId+",姓名:"+stuName+",年齡:"+age;

}

}

class Course{

private int cid; //課程編號

private String cName;

private int credit; ?//學分

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 "課程編號:"+cid+",課程名稱:"+cName+",課程學分:"+credit;

}

}

class StudentCourse{

private Student student;//學生信息

private Course course; ?//課程信息

private double score; ? //成績

public StudentCourse(){};

public StudentCourse(Student student,Course course,double score){//這個為student和course賦值,所以這個不需要set方法

this.student=student;

this.score=score;

this.course=course;

}

public String getStudentCourseInfo(){

return "學生信息"+student+",課程信息"+course+",成績:"+score;

}

//下面這個我認為這個是點睛之筆

public Student getStudent(){

return this.student;

}

public Course getCourse(){

return this.course;

}

? public double getScore(){

?return this.score;

? }

}


正在回答

2 回答

public void setStudentCourse(StudentCourse studentCouse[]){

this.studentCourse=studentCourse;

}

就這段,兄嘚,你的方法中的參數數據studentCourse少了個r

0 回復 有任何疑惑可以回復我~
#1

斗膽請問大俠 提問者

非常感謝!
2018-08-17 回復 有任何疑惑可以回復我~

看了半天,沒看出問題

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

問一個小問題,我for循環那里出現了null pointer我檢查了幾遍都沒出問題

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號