每日代码系列(8)

 1 class Person {
 2   String name;
 3   private int age;
 4   public boolean setAge(int newAge) {
 5     if(newAge>=5&&newAge<=20) {
 6       age=newAge;
 7       return true;
 8     }
 9     else { 
10       return false;
11     }
12   }
13   public int getAge() {
14     return age;      
15   }
16 }

这是首次引入类,刚开始学习。

ps:建议类单独拿出写,不知道为什么这两个写在一起,就会报错,错误详情为:

这一行 import java.util.*; 错误: 需要class, interface或enum
我搜了好久也没解决,希望大神看到之后解答一下!
 1 import java.util.*;
 2 public class App1 {
 3   public static void main(String[] args) {
 4     Person s1=new Person();
 5     s1.name="张三";
 6     int a;
 7     System.out.print("请输入年龄:");
 8     Scanner reader=new Scanner(System.in);
 9     a=reader.nextInt();
10     if(s1.setAge(a)==true) {
11       System.out.println(s1.name+"年龄为:"+a);
12     }
13     else
14       System.out.println("输入错误");
15   }
16 }

这是主方法,废话不多说,你的推荐就是我最大的动力,请多多支持,愿我们一起进步!

原文地址:https://www.cnblogs.com/ljydbk/p/14076907.html