有谁知道main方法中这个载货量或者载重量如何输出显示出来?System.out.println(car.getNo()+"号车,"+car.getName()+",一天租价格:"+car.getPrice()+",载客量:");

来源:12-2 项目问题解析 1

爱上疯狂

2015-07-23 21:31

Car.java代码

public class Car {

private int no;

private String name;

private double price;

public int getNo() {

return no;

}



public void setNo(int no) {

this.no = no;

}



public String getName() {

return name;

}



public void setName(String name) {

this.name = name;

}



public double getPrice() {

return price;

}



public void setPrice(double price) {

this.price = price;

}



public Car(int no, String name, double price) {

super();

this.no = no;

this.name = name;

this.price = price;

}



public Car() {

super();

}


}


PassengerCar .java代码

public class PassengerCar extends Car {

private double passengerCability;


public double getPassengerCability() {

return passengerCability;

}


public void setPassengerCability(double passengerCability) {

this.passengerCability = passengerCability;

}


public PassengerCar(int no, String name, double price,

double passengerCability) {

super(no, name, price);

this.passengerCability = passengerCability;

}

}





import java.util.Scanner;


public class Initinal {

public static void main(String[] args) {

Car c1 = new PassengerCar(1,"班车",200.0,20);

Car c2 = new PassengerCar(2,"客车",350.5,35);

Car c3 = new Trunk(3,"大货车",500.0,5);

Car c4 = new Trunk(4,"小货车",300.0,3);

Car c5 = new PickUp(5,"皮卡车",250.0,10,2);

Car[] carAll = {c1,c2,c3,c4,c5};

System.out.println("你要租车吗?租车输入1,否则输入0:");

Scanner scan = new Scanner(System.in);

String choice = scan.next();

if("1".equals(choice)){

System.out.println("现在所有的车辆有:");

for(Car car:carAll){

if(car instanceof PassengerCar){

System.out.println(car.getNo()+"号车,"+car.getName()+",一天租价格:"+car.getPrice()+",载客量:");

}

if(car instanceof Trunk){

System.out.println(car.getNo()+"号车,"+car.getName()+",一天租价格:"+car.getPrice()+",载货量:");

}

if(car instanceof PickUp){

System.out.println(car.getNo()+"号车,"+car.getName()+",一天租价格:"+car.getPrice()+",载客量:"+",载货量:");

}

}

System.out.println("请输入您要租车的数量:");

int num = scan.nextInt();

int[] nums = new int[num];

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

System.out.println("请选择您要选择租的车的序号:");

nums[i] = scan.nextInt();

}

double sum =0;

System.out.println("您的账单如下:");

for(Car car:carAll){

for(int numAll:nums){

if(car.getNo()==numAll){

System.out.println(car.getNo()+"号车,"+car.getName()+",一天租价格:"+car.getPrice());

sum+=car.getPrice();

}

}

}

System.out.println("总价格为:"+sum+"元");

}else{

System.out.println("谢谢使用!");

}

}


}


写回答 关注

1回答

  • tanhouyusheng
    2015-12-20 13:17:24

    没看懂你的问题是要干什么啊

Java入门第二季 升级版

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

530654 学习 · 6091 问题

查看课程

相似问题