程序改错题。下述代码有错,请参照程序的输出修改程序.
public class Main {
public static void main(String[] args) {
Person[] p = new Person[3];
p[0].name = "zhangsan";
p[0].age = 18;
p[1].name = "lisi";
p[1].age = 20;
p[2].name = "wangwu";
p[2].age = 22;
for (int i = 0; i < p.length; i++) {
System.out.println(p[i]);
}
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
super();
this.name = name;
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;
}
}
输入格式:
无
输出样例:
zhangsan 18
lisi 20
wangwu 22
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Person[] p = new Person[3];
p[0] = new Person("zhangsan", 18);
p[1]=new Person("lisi", 20);
p[2]=new Person("wangwu", 22);
for (int i = 0; i < p.length; i++) {
System.out.println(p[i].getName()+" "+p[i].getAge());
}
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
super();
this.name = name;
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;
}
}