java汽车_java,编写⼀个汽车类
packageorg.hanqi.pn0120;//汽车
public classCar {//颜⾊
privateString yanse;//品牌
privateString pinpai;//油箱容量
private double youxiangrl = 40;//当前油量
private double dangqianyl = 0;public doublegetdangqianyl() {return this.dangqianyl;
}//加油
public void jiaYou(doubleyouliang) {if (youliang < 0) {
System.out.println("不要偷我的油!");
}else if (this.dangqianyl + youliang > uxiangrl) {//本次加油量
double bcyl = uxiangrl - this.dangqianyl;this.dangqianyl = uxiangrl;
System.out.println("油太多了,已经加满了,这次加了" + (bcyl) + "");
}else{this.dangqianyl +=youliang;
System.out.println("这次加了" + youliang + "升油");
}
}//private double youhao=
private double xingshizlc = 0;public doublegetXingshizlc() {return this.xingshizlc;
}//⾏驶
public void xingshi(double xingshilc, doubleyouhao) {if (this.dangqianyl <= 0) {
System.out.println("车没有油,怎么开车");return;
}if (xingshilc < 0 || youhao < 0) {
System.out.println("不可能这么开车");
}else{
}//本次油耗
double dangqyh = xingshilc * youhao / 100;this.dangqianyl -=dangqyh;//this.dangqianyl = this.dangqianyl > 0 ? this.dangqianyl : 0;
if (this.dangqianyl < 0) {this.dangqianyl = 0;
System.out.println("你是推着车⾛么");
}this.xingshizlc +=xingshilc;
System.out.println("本次⾏驶了" + xingshilc + "公⾥");
}publicString getYanse() {returnyanse;
}public voidsetYanse(String yanse) {this.yanse =yanse;
}publicString getPinpai() {returnpinpai;
}public voidsetPinpai(String pinpai) {this.pinpai =pinpai;
}public doublegetYouxiangrl() {returnyouxiangrl;
}public void setYouxiangrl(doubleyouxiangrl) {uxiangrl =youxiangrl;
}public Car(String pinpai, doubleyouxiangrl) {super();this.pinpai =uxiangrl =youxiangrl; }//main⽅法
public static voidmain(String[] args) {
Car myBenchi= new Car("奔驰", 50);
System.out.println("品牌=" +Pinpai());
System.out.println("油箱容量=" +Youxiangrl());
myBenchi.xingshi(200, 13);
myBenchi.jiaYou(30);
myBenchi.jiaYou(30);
System.out.println("当前容量=" +dangqianyl());
myBenchi.xingshi(300, 15);
System.out.println("当前容量=" +dangqianyl());
myBenchi.xingshi(200, 13);
System.out.println("⾏驶总⾥程=" +Xingshizlc());
System.out.println("当前容量=" +dangqianyl());
java怎么编写}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。