题解 | #验证年龄#

验证年龄

https://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){
        if(age < 0){
            this.age = 0;
        }else if(age >= 0 && age <= 200){
            this.age = age;
        }else{
            this.age = 200;
        }
    }

    public int getAge(){
        return age;
    }
    

}

全部评论

相关推荐

点赞 评论 收藏
分享
shtdbb_:还不错,没有让你做了笔试再挂你
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务