一个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());
}
}
} 展开
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());
}
}
} 展开
1个回答
展开全部
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); //这里为啥要在一个循环里调用三次这个方法?
}
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); //这里为啥要在一个循环里调用三次这个方法?
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询