Java上机实践四实验三共饮同井水

  • Post author:
  • Post category:java


package com.company;

public class Main
{
    public static void main(String[] args)
    {
        Village.setWaterAmount(200);
        int leftwater = Village.waterAmount;
        System.out.println("水井中共存有" + leftwater +"升水");
        Village zhaozhuang,majiahezi;
        zhaozhuang = new Village("赵庄");
        majiahezi = new Village("马家河子");
        zhaozhuang.setPeoplenumber(80);
        majiahezi.setPeoplenumber(120);
        zhaozhuang.drinkWater(50);
        leftwater = majiahezi.lookWaterAmount();
        String name = majiahezi.name;
        System.out.println("发现水井中有" + leftwater + "升水");
        majiahezi.drinkWater(100);
        leftwater = zhaozhuang.lookWaterAmount();
        name = zhaozhuang.name;
        System.out.println("发现水井中有" + leftwater + "升水");
        int peoplnumber;
        peoplnumber = zhaozhuang.getPeoplenumber();
        System.out.println(peoplnumber);
        peoplnumber = majiahezi.getPeoplenumber();
        System.out.println(peoplnumber);
    }
}

class Village
{
    static int waterAmount;
    int peoplenumber;
    String name;
    Village(String s)
    {
        this.name = s;
    }
    static void setWaterAmount(int m)
    {
        if(m > 0)
        {
            waterAmount = m;
        }
    }
    void drinkWater(int n)
    {
        if(waterAmount >= 0)
        {
            waterAmount = waterAmount - n;
            System.out.println(name + "喝了" + n + "升水");
        }
        else
            waterAmount = 0;
    }
    static int lookWaterAmount()
    {
        return waterAmount;
    }
    void setPeoplenumber(int n)
    {
        peoplenumber = n;
    }
    int getPeoplenumber()
    {
        return peoplenumber;
    }
}



版权声明:本文为zhangnuanxin520原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。