为了账号安全,请及时绑定邮箱和手机立即绑定

在帮助下,完成了练习。

package com.imooc;

import java.util.Scanner;


public class Initial {


public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println("欢迎使用租车系统\n您是否要租车?1-是,0-否");

Scanner input=new Scanner(System.in);

int isRent=input.nextInt();

double allrent=0;

double allloadage=0;

    int allseats=0;

if(isRent==1) {

System.out.println("您可租车的类型和价目表:");

System.out.println("序号    汽车名称    租金    容量");

Car[] cars= {

new Sedan("哈弗",200,5),

new Sedan("雪佛兰",240,5),

new PickUp("本田",400,3,800.5),

new PickUp("丰田",450,3,1000.5),

new Truck("东风",600,3000),

new Truck("五菱",650,3500)

};

for (int i=0;i<cars.length;i++)

{

int index=i+1;

if(cars[i] instanceof Sedan)

{

Sedan car=(Sedan)cars[i];//强制类型转换

System.out.println(index+"  "+car.CarName+"  "+

car.Rent+"元/天"+" "+"载人: "+car.getSeats()+"人 ");

}

else if(cars[i] instanceof Truck)

{

Truck car=(Truck)cars[i];

System.out.println(index+"  "+car.CarName+"  "+

car.Rent+"元/天"+" "+"载货:"+car.getLoadage()+"kg");

}

else

{

PickUp car=(PickUp)cars[i];

System.out.println(index+"  "+car.CarName+"  "+

car.Rent+"元/天"+" "+"载人: "+car.getSeats()+"人 "+" 载货:"+car.getLoadage()+"kg");

}

}

System.out.println("请输入您想要的租赁的车辆数目:");

int carsnum=input.nextInt();

Car[] chooseCar=new Car[carsnum];

for(int i=0;i<carsnum;i++) {

int num=i+1;

System.out.println("请选择第"+num+"辆车:");

int whichcar=input.nextInt();

if (whichcar<=cars.length&& whichcar>0)

{

chooseCar[i]=cars[whichcar-1];

}

else {

System.out.println("请输入正确的车辆序号.\n");

i--;

continue;

}

}

System.out.println("您一共租了"+carsnum+"辆车");

System.out.println("分别是:");

for(int i=0;i<carsnum;i++)

{

System.out.println((i+1)+":"+chooseCar[i].getCarName());

}

    

for(int i=0;i<carsnum;i++)

{

if(chooseCar[i] instanceof Sedan)

{

Sedan car=(Sedan)chooseCar[i];

allrent+=car.getRent();

    allseats+=car.getSeats();

}

else if(chooseCar[i] instanceof Truck)

{

Truck car=(Truck)chooseCar[i];

allrent+=car.getRent();

allloadage+=car.getLoadage();

}

else

{

PickUp car =(PickUp)chooseCar[i];

allrent+=car.getRent();

allseats+=car.getSeats();

allloadage+=car.getLoadage();

}

}

System.out.println("一共能载:"+allloadage+"kg货物,能载"+allseats+"人.");

System.out.println("一共需要:"+allrent+"元/天.\n");

System.out.println("请输入租赁天数:");

int days=input.nextInt();

System.out.println("您一共租赁"+days+"天,租金是:"+allrent*days+"元.");

}

}


}



正在回答

6 回答

我也懵逼了

0 回复 有任何疑惑可以回复我~

Car[] cars= {

new Sedan("哈弗",200,5),

new Sedan("雪佛兰",240,5),

new PickUp("本田",400,3,800.5),

new PickUp("丰田",450,3,1000.5),

new Truck("东风",600,3000),

new Truck("五菱",650,3500)

};


朋友 能不能解释下,这段是什么意思,前面有那一课有教过吗

0 回复 有任何疑惑可以回复我~

package com.imooc;


/*

 * 皮卡,既能载货又能载人。

 */

public class PickUp extends Car {

protected int seats;

protected double Loadage;

public PickUp(String CarName,double Rent,int seats,double Loadage) {

super(CarName,Rent);

this.seats=seats;

this.Loadage=Loadage;

}

public int getSeats() {

return this.seats;

}

public double getLoadage() {

return this.Loadage;

}


}



1 回复 有任何疑惑可以回复我~

package com.imooc;


/*

 * 货车

 */

public class Truck extends Car {

protected double Loadage;

public Truck(String CarName,double Rent,double Loadage) {

super(CarName,Rent);

this.Loadage=Loadage;

}

public double getLoadage() {

return this.Loadage;

}

}


0 回复 有任何疑惑可以回复我~

package com.imooc;


/*

 * 载人汽车

 */

public class Sedan extends Car {

protected int seats;

public Sedan(String CarName,double Rent,int seats) {

super(CarName,Rent);

this.seats=seats;

}

public int getSeats() {

return this.seats;

}

}



0 回复 有任何疑惑可以回复我~

package com.imooc;


public  class Car {

protected String CarName;

protected double Rent;

public Car(String CarName,double Rent) {

this.CarName=CarName;

this.Rent=Rent;

}

public String getCarName() {

return this.CarName;

}

public Double getRent() {

return this.Rent;

}

}



0 回复 有任何疑惑可以回复我~
#1

qq__8737

为什么不用private
2020-03-22 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Java入门第二季 升级版
  • 参与学习       530711    人
  • 解答问题       6091    个

课程升级!以终为始告别枯燥,在开发和重构中体会Java面向对象编程的奥妙

进入课程

在帮助下,完成了练习。

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信