qq_传说中的怪盗_0
2016-04-10 16:48
我做的JAVA第二季最后的那个租车系统,最后有一个问题搞了很久都搞不定了。 问题出在最后的计算金额,详情如下 文件:Menu.java package com.ybw.sys; import java.util.Scanner; public class Menu { public static void main(String[] args) { System.out.println("欢迎使用达达租车系统!"); Truck[] truck=new Truck[3]; PassengerCar[] passenger=new PassengerCar[3]; PickUp[] pickup=new PickUp[3]; for(int i=0;i<truck.length;i++) truck[i]=new Truck(); truck[0].Truck("T01","卡车", "垃圾大王",10,1000); truck[1].Truck("T02","卡车","公共事业卡车",20,1500); truck[2].Truck("T03","卡车","矿石搬运车",25,1800); for(int i=0;i<passenger.length;i++) passenger[i]=new PassengerCar(); passenger[0].PassengerCar("P01","中小型车","奥北 小辣椒",6,700); passenger[1].PassengerCar("P02","中小型车","卡林 骷髅马",4,500); passenger[2].PassengerCar("P03","中小型车","绝品 先知(敞篷版)",4,600); for(int i=0;i<pickup.length;i++) pickup[i]=new PickUp(); pickup[0].PickUp("PU01","皮卡","叛乱分子",4,2,1000); pickup[1].PickUp("PU02","皮卡","铁尼高",3,1,800); pickup[2].PickUp("PU03","皮卡","绝致 屌王",6,2,800); System.out.println("以下是可以租赁的所有车辆:"); System.out.println("卡车:(ID、类型、型号、载重、租金)"); for(int i=0;i<truck.length;i++) truck[i].show(); System.out.println("客车:(ID、类型、型号、载客、租金)"); for(int i=0;i<passenger.length;i++) passenger[i].show(); System.out.println("皮卡:(ID、类型、型号、载客、载重、租金)"); for(int i=0;i<pickup.length;i++) pickup[i].show(); System.out.println("请问您要租多少辆车:"); Scanner se= new Scanner(System.in); int num=se.nextInt();//输入租车的数量 String[] total=new String[num]; System.out.println("请输入您要租赁的车辆ID:"); for(int i=0;i<num;i++) { Scanner sw= new Scanner(System.in); System.out.println("第"+(i+1)+"辆车的ID:"); total[i]=new String(); total[i]=sw.nextLine(); } System.out.println("您所将租赁的车辆如下:"); int sum=0;//总价 for(int i=0;i<num;i++) { if(total[i].equals(truck[i].ID)) { truck[i].show(); sum=sum+truck[i].price; } //System.out.println("T"+i); } for(int i=0;i<num;i++) { if(total[i].equals(passenger[i].ID)) { passenger[i].show(); sum=sum+passenger[i].price; } //System.out.println("P"+i); } for(int i=0;i<num;i++) { if(total[i].equals(pickup[i].ID)) { pickup[i].show(); sum=sum+pickup[i].price; } // System.out.println("PU"+i); } System.out.println("您所需付款金额为:"+sum+"元"); System.out.println("欢迎下次光临!"); }//main }//class 测试结果是这样的, 比如我输入租3辆车,然后输入这3辆车的ID,如果这3辆车的ID属于同一类(如同属于PassengerCar, 或者都属于Truck或都属于PickUp,结果是正常的。但是如果这3辆车的ID不属于同一类, 那么计算总价时,只会出错。比如输入的ID为 P1,P2,T1,那么运行只会计算P1+P2的价格, 如果输入的ID为,P1,T1,PU1,那么只会计算P1的价格,这个地方我已经卡了一个多小时 了实在不知道怎么弄了,求救。 附上其他几个类的源代码 文件:Vehicle.java package com.ybw.sys; public class Vehicle { public String type;//车种类 public String name;//车名 public String ID;//ID public int price; } 文件:PassengerCar.java package com.ybw.sys; public class PassengerCar extends Vehicle { private int passenger;//载客量 public void PassengerCar(String ID1,String type1,String name1,int passenger1,int price1) { ID=ID1; type=type1; name=name1; passenger=passenger1; price=price1; } public void show() { System.out.printf(ID+"\t"); System.out.printf(type+"\t "); System.out.printf(name+"\t "); System.out.printf(passenger+"人\t "); System.out.printf(price+"元\t "); System.out.println(" "); } } 文件:Truck.java package com.ybw.sys; //货车 public class Truck extends Vehicle { private float carry;//载货量 public void Truck(String ID1,String type1,String name1,float carry1,int price1) { ID=ID1; type=type1; name=name1; carry=carry1; price=price1; } public void show() { System.out.printf(ID+"\t"); System.out.printf(type+"\t "); System.out.printf(name+"\t "); System.out.printf(carry+"吨\t "); System.out.printf(price+"元\t "); System.out.println(" "); } } 文件:PickUp.java package com.ybw.sys; public class PickUp extends Vehicle { private int passenger; private float carry; public void PickUp(String ID1,String type1,String name1,int passenger1,float carry1,int price1) { ID=ID1; type=type1; name=name1; carry=carry1; passenger=passenger1; price=price1; } public void show() { System.out.printf(ID+"\t"); System.out.printf(type+"\t"); System.out.printf(name+"\t"); System.out.printf(passenger+"人\t"); System.out.printf(carry+"吨\t"); System.out.printf(price+"元\t"); System.out.println(" "); } }
机智如我自己解决了
没人吗????
Java入门第二季 升级版
530671 学习 · 6091 问题
相似问题