答答系统搞定,代码很凌乱,求高人指点!
代码中现有无法解决的问题: 1、main方法中定义的两个存放客车和货车名称的数组,想根据用户输入来定义数组的长度,不知道是定义的位置不对还是什么情况,程序运行会出现 溢出错误; 2、打印货车和客车名称的时候,出现重名的情况想实现 汽车名称*数量 的方式显示不知该如何解决!
代码如下:
package com.imooc.carSystem; //父类
public class Car {
String name;
int money;
int rl;
static int id;
void print() {
id++;
System.out.println(id + ". " + name + "\t" + money + "元/天 载人:" + rl
+ "人");
}
}
package com.imooc.carSystem; //客车继承父类
public class Bus extends Car {
Bus(String name,int money,int rl){
this.name=name;
this.money=money;
this.rl=rl;
}
}
package com.imooc.carSystem; //货车继承父类
public class HuoChe extends Car {
HuoChe(String name,int money,int rl){
this.name=name;
this.money=money;
this.rl=rl;
}
void print() {
id++;
System.out.println(id + ". " + name + "\t" + money + "元/天 载货:" + rl
+ "吨");
}
}
package com.imooc.carSystem; //皮卡车继承
public class Pka extends Car{
int rl2;
Pka(String name,int money,int rl){
this.name=name;
this.money=money;
this.rl=rl;
}
void print() {
id++;
System.out.println(id + ". " + name + "\t" + money + "元/天 载人:" + rl
+ "人"+" 载货"+rl2+"吨");
}
}
package com.imooc.carSystem; //主方法
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int zkl = 0, day = 0;
float zhl=0f,jg=0f;
String[] huoche = new String[100];
String[] bus = new String[100];
System.out.println("欢迎使用答答租车系统:");
System.out.println("您是否要租车:1是 0否");
Scanner scanner1 = new Scanner(System.in);
if (scanner1.nextInt() == 1) {
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号 汽车名称 租金 容量");
Bus bus1 = new Bus("奥迪A4", 500, 4);
bus1.print();
Bus bus2 = new Bus("马自达6", 400, 4);
bus2.print();
Pka pka = new Pka("皮卡雪6", 450, 2);
pka.rl2 = 2;
pka.print();
Bus bus3 = new Bus("金龙", 800, 20);
bus3.print();
HuoChe huoche1 = new HuoChe("松花江", 400, 4);
huoche1.print();
HuoChe huoche2 = new HuoChe("依维柯", 1000, 20);
huoche2.print();
System.out.println("请输入租车的数量:");
Scanner scanner2 = new Scanner(System.in);
int carNum = scanner2.nextInt();
// 判断输入的租用的车辆数值是否有效。
if (carNum > 0) {
int hcNum=0,busNum=0;
// 循环输入选择车辆。
for (int i = 1; i <= carNum; i++) {
System.out.println("请输入第" + i + "两车的序号:");
Scanner scanner3 = new Scanner(System.in);
int carId = scanner3.nextInt();
// 判断输入的车辆序号是否有效
if (carId > 0 && carId <= 6) {
// 将所选择车辆的名称存入carTaype[]中。
switch (carId) {
case 1:
bus[busNum] = bus1.name;
busNum=busNum+1;
zkl+=bus1.rl;
jg=jg+500;
break;
case 2:
bus[busNum]=bus2.name;
busNum=busNum+1;
zkl+=bus2.rl;
jg=jg+400;
break;
case 3:
bus[busNum] = pka.name;
busNum=busNum+1;
huoche[hcNum] = pka.name;
hcNum=hcNum+1;
zkl+=pka.rl;
zhl=+pka.rl2;
jg=jg+450;
break;
case 4:
bus[busNum] = bus3.name;
busNum=busNum+1;
zkl+=bus3.rl;;
jg=jg+800;
break;
case 5:
huoche[hcNum]= huoche1.name;
hcNum=hcNum+1;
zhl=zhl+huoche1.rl;;
jg=jg+400;
break;
case 6:
huoche[hcNum] = huoche2.name;
hcNum=hcNum+1;
jg=jg+1000;
zhl=zhl+huoche2.rl;
break;
}
} else {
System.err.println("车辆序号大于0小于等于6");
// 车辆ID不是有效值时结束for循环
return;
}
}
System.out.println("请输入租车天数");
Scanner scanner4 = new Scanner(System.in);
day = scanner4.nextInt();
if (day > 0) {
System.out.println("您的账单:");
System.out.println("***可载人的车有:");
for (int j = 0; j < busNum; j++) {
System.out.print(" " + bus[j]);
}
System.out.print(" 共载人:" + zkl+"人");
System.out.println("\n***载货车有:");
for (int k = 0; k < hcNum; k++) {
System.out.print(" " + huoche[k]);
}
System.out.print(" 共载货:" + zhl+"吨\n");
System.out.println("***租车总价格:" + jg*day + "元");
} else {
System.err.println("请输入有效天数");
}
} else {
System.err.println("租车数量大于0");
}
} else {
System.err.println("谢谢使用!");
}
}
}