波浪波浪海海
2016-01-26 23:02
不过遗憾的是无法将重复的载客车或载货车不重复输出,请问有大神可以解决吗,代码如下:
package Car; public class Car { private String name; private int money; private int passagerCapacity; private int cargoCapacity; public Car(String name,int money,int passagerCapacity,int cargoCapacity){ this.name=name; this.money=money; this.passagerCapacity=passagerCapacity; this.cargoCapacity=cargoCapacity; } public String getName(){ return name; } public int getMoney(){ return money; } public int getPassagerCapacity(){ return passagerCapacity; } public int getCargoCapacity(){ return cargoCapacity; } }
package Car; public class PickupCar extends Car{ public PickupCar(String name,int money,int passagerCapacity,int cargoCapacity){ super(name,money,passagerCapacity,cargoCapacity); } }
package Car; public class PickUpSnow6 extends Car{ public PickUpSnow6(String name,int money,int passagerCapacity,int cargoCapacity){ super(name,money,passagerCapacity,cargoCapacity); } }
package Car; import java.util.ArrayList; import java.util.Scanner; public class User { public void play() { System.out.println("欢迎使用答答租车系统:"); System.out.println("您是否要租车:1是0否"); Scanner input = new Scanner(System.in); int yesOrNo = input.nextInt(); if(yesOrNo == 1){ Car [] carList ={new PickupCar("AudiA4\t",500,4,0),new PickupCar("Mazda6\t",400,4,0),new PickUpSnow6("PickUpSnow6",450,4,2),new PickupCar("GoldenDragonBus",800,20,0),new Truck("SongHuaRiver",400,0,4),new Truck("Iveco\t",1000,0,20)}; System.out.println("您可租车的类型及其价目表:"); System.out.println("序号"+"\t"+"汽车名称"+ "\t\t"+ "租金(元/天)"+"\t"+"容量"); for(int i = 0;i<carList.length;i++){ System.out.println((i+1)+".\t"+carList[i].getName()+"\t"+carList[i].getMoney()+"\t"+"载人:"+carList[i].getPassagerCapacity()+"人,载货:"+carList[i].getCargoCapacity()+"吨"); } System.out.println("请输入您要的汽车的数量:"); int num = input.nextInt(),i = 0; ArrayList<Car> userList= new ArrayList<Car>(); int cost1=0,cost2=0; int cap1=0,cap2=0; String pickupCarName = ""; String truckName = ""; while (i < num) { System.out.println("请输入第" + (i + 1) + "车的序号:"); int choose = input.nextInt()-1; if(choose<0||choose>5){ System.out.println("输入错误,请再次输入序号:"); }else{ Car currentCar = carList[choose]; userList.add(currentCar); if(currentCar instanceof PickupCar){ pickupCarName += currentCar.getName()+" "; cost1 += currentCar.getMoney(); cap1 += currentCar.getPassagerCapacity(); i++; }else if(currentCar instanceof PickUpSnow6){ pickupCarName += currentCar.getName()+" "; truckName += currentCar.getName()+" "; cost1 += currentCar.getMoney(); cap1 += currentCar.getPassagerCapacity(); cap2 += currentCar.getCargoCapacity(); i++; }else{ truckName += currentCar.getName()+" "; cost2 += currentCar.getMoney(); cap2 += currentCar.getCargoCapacity(); i++; } } } System.out.println("请输入租车天数:"); int day = input.nextInt(); if(day <= 0){ System.out.println("输入有误,请输入租车天数:"); day = input.nextInt(); } System.out.println("您的账单:"); System.out.println("***可载人的车有:" + pickupCarName + "共载人:" + cap1 +"人"); System.out.println("***可载货的车有:" + truckName + "共载货:" + cap2 +"吨"); System.out.println("***租车总价格:" + day*(cost1 + cost2) +"元"); }else{ System.out.println("好的,已退出该系统"); } input.close(); } }
package Car; public class UserTest { public static void main(String[] args) { User u1 = new User(); u1.play(); } }
int[] cheShu = {0,0,0,0,0};
for(int i = 0 ;i<5;i++){
for(int j=0;j<che.size();j++){ //che就是你定义的那个动态数组,也就是选择的车
if(carList[i]==che.get(j)){
cheShu[i]+=1;
}
}
}
carList中每种类型车的选了几辆都对应记在cheShu里了
输出时候可以判断大于0就输出
计算价格什么的 che.价格 * cheShu[i] 就好了
大哥,适当地使用 缩进 好不-_-!
如果不想重复输出不同类型的车辆,那么在使用userList.add()之前,至少要先行判断,确保一辆车,只加入一个租用类型车数组中。如果判断条件较多,建议使用switch,至少看起来会舒服很多。
Java入门第二季 升级版
530655 学习 · 6091 问题
相似问题