看我这个可以,代码给你:
public static void main(String[] args) {
Scanner sc = new Scanner( System.in);
Student[] sarr = new Student[3];
Student s = new Student();
for (int i = 0; i < sarr.length; i++) {
s.setName(sc.nextLine());
String a = s.getName();
sarr = new Student(a);
s.setAge(sc.nextInt());
int b = s.getAge();
sarr = new Student(b);
}
for (int i = 0; i < sarr.length; i++) {
System.out.println("姓名: "+ sarr.getName() + "年龄:" + sarr.getAge());
}
}
}
class Student{
private String name;
private int age;
public Student(){} //空参构造
public Student(String name) { //有参构造
this.name = name;
}
public Student(int age){
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
} |