题解 | #验证年龄#
验证年龄
http://www.nowcoder.com/practice/fdb94493ebc34eef8d9b01dea47685b9
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)//设置setter方法,按照要求对形参age进行判断,然后给当前对象this的属性age赋值
{
if(age>200)
this.age=200;
else if(age<0)
this.age=0;
else
this.age=age;
}
public int getAge()
{
return this.age;//getter方法直接返回当前对象的属性age即可,this可省略不写
}
}