一个java小程序的问题。不知道为什么会出现异常。

abstractclassStudent{finalstaticintCourseNo=3;Stringname;Stringtype;int[]courses;Stri... abstract class Student {
final static int CourseNo = 3;
String name;
String type;
int[] courses;
String courseGrade;

public Student(String name) {
this.name = name;
courses = new int[CourseNo];
courseGrade = null; //代码1 初始化courseGrade为空串
}
public abstract void calculateGrade();
public String getName(){
return name;
}
public String getType(){
return type;
}
public String getCourseGrade(){
return courseGrade;
}
public int getCourseScore(int courseNumber){
return courses[courseNumber];
}
public void setName(String name){
this.name = name;
}
public void setType(String type){
this.type = type;
}
public void setCourseScore(int courseNumber,int courseScore){
this.courses[courseNumber]=courseScore;
}
}

class Undergraduate extends Student{
public Undergraduate(String name){
super(name);
type = "本科生";
}
public void calculateGrade(){
int total = 0;
double average = 0;
for(int i = 0;i<CourseNo;i++){
total = total+this.courses[i];//累加各门课程成绩
}
average = total/CourseNo;
if(average>=80&&average<100)courseGrade ="优秀";
else if(average>=70&&average<80)courseGrade ="良好";
else if(average>=60&&average<70)courseGrade ="一般";
else if(average>=50&&average<60)courseGrade ="及格";
else courseGrade = "不及格";
}
}
class Postgraduate extends Student{
public Postgraduate(String name){
super(name);
type = "研究生";
}
public void calculateGrade(){
int total = 0;
double average = 0;
for(int i=0;i<CourseNo;i++){
total += courses[i];
}
average = total/CourseNo;
if(average>=90&&average<100)courseGrade ="优秀";
else if(average>=80&&average<90)courseGrade ="良好";
else if(average>=70&&average<80)courseGrade ="一般";
else if(average>=60&&average<70)courseGrade ="及格";
else courseGrade = "不及格";
}
}
public class Polymorphism {
public static void main(String[] args) {
Student[] students = new Student[5];
students[0] = new Undergraduate("陈建平");
students[1] = new Undergraduate("鲁向东");
students[2] = new Postgraduate("匡晓华");
students[3] = new Undergraduate("周丽娜");
students[2] = new Postgraduate("梁欣欣");
for(int i=0;i<5;i++){
students[i].setCourseScore(0,87);
students[i].setCourseScore(1,90);
students[i].setCourseScore(2,78);
}
for(int i = 0;i<5;i++){
students[i].calculateGrade();
}
System.out.println("姓名"+" 类型"+" 成绩");
System.out.println("----------------------------");
for(int i = 0;i<5;i++){
System.out.println(students[i].getName()+" "+ students[i].getType()+" "+students[i].getCourseGrade());
}
}
}
展开
 我来答
莔囧冏冋冂
2012-05-15 · TA获得超过642个赞
知道小有建树答主
回答量:411
采纳率:100%
帮助的人:549万
展开全部
Student[] students = new Student[5];
students[0] = new Undergraduate("陈建平");
students[1] = new Undergraduate("鲁向东");
students[2] = new Postgraduate("匡晓华");
students[3] = new Undergraduate("周丽娜");
students[2] = new Postgraduate("梁欣欣"); // 错了吧,这里覆盖了students[2]
for(int i=0;i<5;i++){ //导致循环里 students[4] 出现空指针异常
students[i].setCourseScore(0,87);
students[i].setCourseScore(1,90);
students[i].setCourseScore(2,78); //这里为啥要在一个循环里调用三次这个方法?
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式