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

    科技2025-01-17  5

    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; } }

     

    Processed: 0.011, SQL: 8