大佬看看 有什么要改的地方

来源:12-1 综合练习

慕粉4382624

2018-12-06 20:36

Car页

package com;

public abstract class Car {
    protected String name;
    protected int price;
    protected int style;
    public Car(int style,String name,int price){
        this.name=name;
        this.price=price;
        this.style=style;
    }
    public abstract void showcars();



Truruck页

package com;

public class Truck extends Car{
    protected int weight;
    public Truck(int style,String name,int price,int weight){
         super(style,name,price);
         this.weight=weight;
    }

    @Override
    public void showcars() {
            System.out.println(this.name + " "+this.price+"元/天"+" 载货:"+this.weight+"吨");
    }
}

Bus页

package com;

public class Bus extends Car{
    protected int num;
    public Bus(int style,String name,int price,int num){
        super(style,name,price);
        this.num=num;
    }

    @Override
    public void showcars() {
        System.out.println(this.name + " "+this.price+"元/天"+" 载人:"+this.num+"人");
    }
}



Pickup页

package com;

public class Pickup extends Car{
    protected int weight;
    protected int num;
    public Pickup(int style,String name,int price,int num,int weight){
        super(style,name,price);
        this.num=num;
        this.weight=weight;
    }

    @Override
    public void showcars() {
        System.out.println(this.name + " "+this.price+"元/天"+" 载人:"+this.num+" 载货:"+this.weight+"吨");
    }
}


main页

package com;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("欢迎使用租车系统,请问是否需要租车:1是 0否");
        int index = input.nextInt();
        if (index == 1) {
            Car[] cars = {new Bus(1,"奥迪A4", 500, 4), new Bus(1,"马自达6", 400, 4),
                    new Bus(1,"金龙", 800, 20),new Pickup(0,"皮卡雪", 450, 4, 2),
                    new Truck(2,"松花江", 400, 4), new Truck(2,"依维柯", 1000, 20)};

            System.out.println("您可租车的类型和价目表:");
            System.out.println("序号  汽车名称  租金  容量");
            for (int i = 1; i <= cars.length; i++) {
                System.out.print(i + " ");
                cars[i - 1].showcars();
            }
            System.out.println("请输入需要的数量");
//            车辆数量
            int carNum = input.nextInt();

//            可载人的车
            String bus="";
//            载人数量
            int nums=0;

//            载货的车
            String trunk="";
//            载货重量
           int weights=0;
//           总价
            int allPrice=0;
            for (int i = 1; i <= carNum; i++) {
                System.out.println("请输入第" + i + "辆车的序号");
                //车辆序号 carIndex-1
                int carIndex = input.nextInt();
                if(cars[carIndex-1].style==0){
//                    载人和载物的车
                    nums+=((Pickup) cars[carIndex-1]).num;
                    weights+=((Pickup) cars[carIndex-1]).weight;
                    bus+=" "+cars[carIndex-1].name;
                }else if(cars[carIndex-1].style==1){
//                    载人的车
                    bus+=" "+cars[carIndex-1].name;
                    nums+=((Bus) cars[carIndex-1]).num;
                }else if(cars[carIndex-1].style==2){
//                    载物的车
                    trunk+=" "+ cars[carIndex-1].name;
                    weights+=((Truck) cars[carIndex-1]).weight;
                }
                allPrice+=cars[carIndex-1].price;
            }
            System.out.println("请输入租车天数");
//            租车天数
            int carDay = input.nextInt();
            allPrice*=carDay;
            System.out.println("您的账单:");
            System.out.println("***可载人的车有:");
            System.out.println(bus+" 共载:"+nums+"人");
            System.out.println("***可载货的车有:");
            System.out.println(trunk+" 共载:"+weights+"吨");
            System.out.println("***租车价格:" + allPrice+"元");
        }
    }
}


写回答 关注

1回答

  • 刘大洋_慕Boy
    2019-06-27 09:39:02

    问题就在Car页,后面少了个大括号啊。是你没复制过来?

Java入门第二季 升级版

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

531023 学习 · 6160 问题

查看课程

相似问题