那个姓名没有运行出来,这种如何完善,求浴室收费问题,就类和对象关系
public class ShowerCharge {
private int id;
private String name;
private double money;
private double price=0.5;
public ShowerCharge(int id,String name,double money,double price){
this.id = id;
this.name = name;
this.money = money;
this.price = price;
}
public void charge(double waterConsumption){
double m = waterConsumption*price;
System.out.println("姓名:"+name);
System.out.println("本次应收费:"+m);
if(money-m>0){
money -=m;
System.out.println("余额"+money);
} else {
money = 0;
System.out.println("余额不足请充值:"+(m-money));
}
}
public static void main(String[] args) {
ShowerCharge showerCharge = new ShowerCharge(1,"贾",100,0.5);
showerCharge.charge(100);
showerCharge.charge(100);
showerCharge.charge(101);
}
}
你在输出也没有加名字啊
输出以下就好啦
你在要显示姓名的println语句里加上this.name就可以了