题解 | #验证年龄#
验证年龄
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刷题,或许会有其他方面