java创建对象数组的空指针异常
publicclassStudent{intscore;doublesum;intmax;publicvoidavr(Student[]stus){for(inti=0;...
public class Student {
int score;
double sum;
int max;
public void avr(Student[] stus){
for(int i=0;i<stus.length;i++){
sum+=stus[i].score;
}
System.out.println("平均成绩:"+sum/stus.length);
}
public void max(Student[] stus){
max=stus[0].score;
for(int i=0;i<stus.length;i++){
if(max<stus[i].score){
max=stus[i].score;
}
System.out.println("最高成绩:"+max);
}
}
}
import java.util.*;
public class Text {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
Student s1=new Student();
Student[] stus=new Student[5];
System.out.println("请输入五名参赛者的成绩:");
for(int i=0;i<stus.length;i++){
stus[i].score=input.nextInt();
}
s1.avr(stus);
}
}
在第二个里的for循环里报错了Exception in thread "main" java.lang.NullPointerException
求解 展开
int score;
double sum;
int max;
public void avr(Student[] stus){
for(int i=0;i<stus.length;i++){
sum+=stus[i].score;
}
System.out.println("平均成绩:"+sum/stus.length);
}
public void max(Student[] stus){
max=stus[0].score;
for(int i=0;i<stus.length;i++){
if(max<stus[i].score){
max=stus[i].score;
}
System.out.println("最高成绩:"+max);
}
}
}
import java.util.*;
public class Text {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
Student s1=new Student();
Student[] stus=new Student[5];
System.out.println("请输入五名参赛者的成绩:");
for(int i=0;i<stus.length;i++){
stus[i].score=input.nextInt();
}
s1.avr(stus);
}
}
在第二个里的for循环里报错了Exception in thread "main" java.lang.NullPointerException
求解 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询