题解 | #验证年龄#

验证年龄

https://www.nowcoder.com/practice/fdb94493ebc34eef8d9b01dea47685b9

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Person p = new Person();
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()) {
            p.setAge(sc.nextInt());
            System.out.println(p.getAge());
        }
    }

}

class Person {

    private int age;

    public Person() {
    }

    public Person(int age) {
        if(age<0){
            this.age=0;
        }else if(age>200){
            this.age=200;
        }else {
            this.age = age;
        }
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if(age<0){
            this.age=0;
        }else if(age>200){
            this.age=200;
        }else {
            this.age = age;
        }
    }
}

java相关 文章被收录于专栏

java刷题,或许会有其他方面

全部评论

相关推荐

03-06 17:17
门头沟学院 Java
程序员小白条:专升本提前注明,不然=白费,到最后面完,告诉你不能过,,还有这开源怎么前端大于后端....短链只写三个功能亮点的话,而且还是经典项目,反而可以不用,要么多写点东西,每个实习,项目都标准3点....
26届求职交流
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务