题解 | #验证年龄#
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Person p = new Person();
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextInt()) {
int age = scanner.nextInt();
p.setAge(age);
System.out.println(p.getAge());
}
}
}
class Person {
private int age;
//write your code here......
public void setAge( int age ){//输入函数设定为无返回值
this.age = age;//this指向此Person类中的age值,age为输入的年龄值
}
public int getAge(){//输出函数设定为int值
if(this.age < 0) age = 0;
else if(this.age > 200) age = 200;
return age;
}
}