public class Student {
private String name ; //姓名
public void setName(String str){ //设置姓名
name = str;

}
public String getName(){//返回姓名
return name;
}
}
import java.util.Scanner;
public class CountScore {

Student[] stud = new Student[5];  //创建5个学生对象
         Scanner sc = new Scanner(System.in);
         for(int i=0; i<5; i++){
             stud[i].setName = sc.next(); //输入学生姓名
         }
         //省略 {System.out.println(stud[0]);} //显示 为null  
public class TestStudentScore {
public static void main(String[] args) { Student[] stud = new Student[3]; System.out.println(stud[0]); //显示为 null
}
}
为什么new了student  怎么显示没有创建对象
请大家帮帮我,是怎么回事?怎么解决该问题。

解决方案 »

  1.   

    Student[] stud = new Student[5];  //创建5个学生对象
             Scanner sc = new Scanner(System.in);
             for(int i=0; i<5; i++){
                 stud[i].setName = sc.next(); //输入学生姓名
             }你只是new出来了这个数组,而没有给这个数组添加student对象,应该循环new出来student添加到数组中
      

  2.   

           for(int i=0; i<5; i++){
                 stud[i]=new Student();
                 stud[i].setName = sc.next(); //输入学生姓名
             }
    加一句
      

  3.   

    是这样吗? for (int i=0; i<5; i+=){
        Student[] stud = new Student[i];
      }