实验代码
Tank.java
package lab2;public class Tank {private double speed;private int bulleAmount;void speedUp(int s) {speed = s + speed;}void speedDown(int d) {if (speed - d >= 0) {speed = speed - d;} else {speed = 0;}}void setBulleAmount(int m) {bulleAmount = m;}public int getBulleAmount() {return bulleAmount;}public double getSpeed() {return speed;}void fire() {if (bulleAmount >= 1) {bulleAmount = bulleAmount - 1;System.out.println("打出一发炮弹");} else {System.out.println("没有炮弹了,无法开火");}}
}
Fight.java
package lab2;public class Fight {public static void main(String[] args) {Tank tank1 = new Tank();Tank tank2 = new Tank();tank1.setBulleAmount(10);tank2.setBulleAmount(10);System.out.println("tank1的炮弹数量:" + tank1.getBulleAmount());System.out.println("tank2的炮弹数量:" + tank2.getBulleAmount());tank1.speedUp(80);tank2.speedUp(90);System.out.println("tank1目前速度:" + tank1.getSpeed());System.out.println("tank2目前速度:" + tank2.getSpeed());tank1.speedDown(15);tank2.speedDown(30);System.out.println("tank1目前速度:" + tank1.getSpeed());System.out.println("tank2目前速度:" + tank2.getSpeed());System.out.println("tank1 开火:");tank1.fire();System.out.println("tank2 开火:");tank2.fire();tank2.fire();System.out.println("tank1的炮弹数量:" + tank1.getBulleAmount());System.out.println("tank2的炮弹数量:" + tank2.getBulleAmount());}
}