public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name,int age) {
this.name=name;
this.age=age;
}
public void setName(String name) {
this.name=name;
}
public String getName() {
return name;
}
public void setAge(int Age) {
this.age=age;
}
public int getAge() {
return age;
}
public static void main(String[] args) {
}
}
public class StudentDemo02 {
public static void main(String[] args) {
ArrayList<Student> array = new ArrayList<Student>();
addStudent(array);
addStudent(array);
addStudent(array);
for(int i=0;i<array.size();i++) {
Student s = array.get(i);
System.out.println(s.getName()+","+s.getAge());
}
}
public static void addStudent(ArrayList<Student> array) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生姓名:");
String name = sc.nextLine();
System.out.println("请输入学生年龄:");
int age = sc.nextInt();
Student s = new Student();
s.setName(name);
s.setAge(age);
array.add(s);
}
}