分享一下代码求大佬给一点优化思路
import java.util.Scanner; import java.lang.System; //车的抽象类 public abstract class car { String carName;//车名 int persons;//载客量 double weights;//载货量 int rent;//租金 //给客车使用的构造方法 public car(String carName,int persons,int rent) { super(); this.carName=carName; this.persons=persons; this.rent=rent; } //给货车使用的构造方法 public car(String carName,double weights,int rent) { super(); this.carName=carName; this.weights=weights; this.rent=rent; } //给皮卡使用的构造方法 public car(String carName,double weights,int persons,int rent) { super(); this.carName=carName; this.weights=weights; this.persons=persons; this.rent=rent; } abstract void show(); } //客车类 public class passengerCar extends car { public passengerCar(String carName,int persons,int rent) { super(carName,persons,rent); } //重写父类show方法。 public void show(){ System.out.println(carName+" "+rent+"元/天 载人:"+persons+"人"); } } //货车类 public class TruckCar extends car { public TruckCar (String carName,double weights,int rent){ super(carName,weights,rent); } public void show(){ System.out.println(carName+" "+rent+"元/天 载货:"+weights+"吨"); } } //皮卡类 public class pickupCar extends car { public pickupCar (String carName,double weights,int persons,int rent){ super(carName,weights,persons,rent); } public void show(){ System.out.println(carName+" "+rent+"元/天 载人:"+persons+"人,载货:"+weights+"吨"); } } //租车前台系统类 public class carRentSystem { public static void main(String[] args) { car[] cars={new TruckCar ("松花江",4.0, 400),new TruckCar ("依维柯",20.0,1000),new pickupCar ("皮卡雪", 4.0, 2, 450),new passengerCar ("马自达", 4, 400),new passengerCar ("奥迪A4", 4, 500)}; System.out.println("欢迎使用嗒嗒租车系统:"); System.out.println("您是否要租车:1是 0否"); Scanner s1=new Scanner(System.in); int xz=s1.nextInt();//捕捉用户选择 if (xz==1) { System.out.println("您可租车的类型及其价目表:"); System.out.println("序号 汽车名字 租金 容量"); for (int i = 0; i < cars.length; i++) { System.out.print(i+" "); cars[i].show(); } System.out.println("请输入您要租汽车的数量:"); int sl=s1.nextInt();//捕捉用户输入的数量 int[] sum=new int[sl];//用来存储用户输入的车的序号。 for (int i = 0; i < sl; i++) { System.out.printf("请输入第%d辆车的序号:",i+1); sum[i]=s1.nextInt();//捕捉用户输入车的序号并存入数组 } System.out.println("请输入租车天数:"); int days=s1.nextInt();//捕捉用户输入的天数 System.out.println("您的账单:"); new carRentSystem ().bill(sum,cars,days); }else { System.out.println("再见"); System.exit(0); } } //生存账单 public void bill(int[] sum,car[] cars,int days) { int persons=0;//统计载客总数 double weights=0;//统计载货总数 int rents=0;//统计总金额 System.out.println("***可载人的车有:"); for (int i = 0; i < sum.length; i++) { //判断这个序号的车是否是客车不是就跳过这次循环 if (sum[i]>2) { System.out.print(cars[sum[i]-1].carName+" "); persons+=cars[sum[i]-1].persons; rents+=cars[sum[i]-1].rent; }else { continue; } } System.out.println("共载人:"+persons+"人"); System.out.println("***可载货的车有:"); for(int i=0;i< sum.length; i++) { if (sum[i]<4) { System.out.print(cars[sum[i]-1].carName+" "); weights+=cars[sum[i]-1].weights; //防止皮卡的金额被二次计算 if (sum[i]!=3) { rents+=cars[sum[i]-1].rent; } } } System.out.println("共载货:"+weights+"吨"); System.out.println("租车总价格:"+rents*days+"元"); } }
代码我都是从eclipse一行一行抄下来的(这个竟然直接粘贴会写成一行),不过在这上面运行不了,应该是类名不对(他所有的类名都是HelloWorld的),不过我把运行效果截图了,求大佬帮我看看有什么可以优化的地方。