在幫助下,完成了練習(xí)。
package com.imooc;
import java.util.Scanner;
public class Initial {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("歡迎使用租車系統(tǒng)\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("您可租車的類型和價(jià)目表:");
System.out.println("序號(hào)? ? 汽車名稱? ? 租金? ? 容量");
Car[] cars= {
new Sedan("哈弗",200,5),
new Sedan("雪佛蘭",240,5),
new PickUp("本田",400,3,800.5),
new PickUp("豐田",450,3,1000.5),
new Truck("東風(fēng)",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];//強(qiáng)制類型轉(zhuǎn)換
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("請輸入您想要的租賃的車輛數(shù)目:");
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("請輸入正確的車輛序號(hào).\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("請輸入租賃天數(shù):");
int days=input.nextInt();
System.out.println("您一共租賃"+days+"天,租金是:"+allrent*days+"元.");
}
}
}
2020-03-27
我也懵逼了
2020-02-19
Car[] cars= {
new Sedan("哈弗",200,5),
new Sedan("雪佛蘭",240,5),
new PickUp("本田",400,3,800.5),
new PickUp("豐田",450,3,1000.5),
new Truck("東風(fēng)",600,3000),
new Truck("五菱",650,3500)
};
朋友 能不能解釋下,這段是什么意思,前面有那一課有教過嗎
2020-02-12
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;
}
}
2020-02-12
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;
}
}
2020-02-12
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;
}
}
2020-02-12
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;
}
}