【JAVA】共同的森林

Village.java

class Village{
        static int treeAmount;
        int peopleNumber;
        String name;
        Village(String s){
            name = s;
        }
        void treePlanting(int n){
            treeAmount = treeAmount+n;
            System.out.println(name+"植树"+n+"棵");
        }
        void fellTree(int n){
            if(treeAmount-n>=0){
                treeAmount = treeAmount-n;
                System.out.println(name+"伐树"+n+"棵");
            }
            else{
                System.out.println("无树木可伐");
            }
        }
        static int lookTreeAmount(){
            return treeAmount;
        }
        void addPeopleNumber(int n){
            peopleNumber = peopleNumber+n;
            System.out.println(name+"增加了"+n+"人");
        }
        
                }

MainClass.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author 高晶
 */
public class MainClass {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Village zhaoZhuang,maJiaHeZhi;
        zhaoZhuang=new Village("赵庄");
        maJiaHeZhi=new Village("马家河子");
        zhaoZhuang.peopleNumber=100;
        maJiaHeZhi.peopleNumber=150;
        Village.treeAmount=200;
        int leftTree = Village.treeAmount;
        System.out.println("森林中有"+leftTree+"棵树");
        zhaoZhuang.treePlanting(50);
        leftTree=Village.lookTreeAmount();g
        System.out.println("森林中有"+leftTree+"棵树");
        maJiaHeZhi.fellTree(70);
        leftTree=Village.lookTreeAmount();
        System.out.println("森林中有"+leftTree+"棵树");
        System.out.println("赵庄的人口"+zhaoZhuang.peopleNumber);
        zhaoZhuang.addPeopleNumber(12);
        System.out.println("赵庄的人口"+zhaoZhuang.peopleNumber);
        System.out.println("马家河子的人口"+maJiaHeZhi.peopleNumber);
        maJiaHeZhi.addPeopleNumber(10);
        System.out.println("马家河子的人口"+maJiaHeZhi.peopleNumber);
        // TODO code application logic here
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值