哈哈~終于寫出來答答打車系統(tǒng)啦,但會重復(fù)輸出載客車和載貨車,代碼如下,希望交流!
不過遺憾的是無法將重復(fù)的載客車或載貨車不重復(fù)輸出,請問有大神可以解決嗎,代碼如下:
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("歡迎使用答答租車系統(tǒng):"); 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("請輸入您要的汽車的數(shù)量:"); 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("請輸入租車天數(shù):"); int?day?=?input.nextInt(); if(day?<=?0){ System.out.println("輸入有誤,請輸入租車天數(shù):"); 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("好的,已退出該系統(tǒng)"); } input.close(); } }
package?Car; public?class?UserTest?{ public?static?void?main(String[]?args)?{ User?u1?=?new?User(); u1.play(); } }
2016-02-18
int[] cheShu = {0,0,0,0,0};??????????????????????
???for(int i = 0 ;i<5;i++){
????for(int j=0;j<che.size();j++){????????? //che就是你定義的那個動態(tài)數(shù)組,也就是選擇的車
?????if(carList[i]==che.get(j)){????????????????????
??????cheShu[i]+=1;
?????}
????}
}
carList中每種類型車的選了幾輛都對應(yīng)記在cheShu里了
輸出時候可以判斷大于0就輸出
計算價格什么的 che.價格 * cheShu[i] 就好了
2016-02-09
大哥,適當(dāng)?shù)厥褂?縮進 好不-_-!
2016-01-27
如果不想重復(fù)輸出不同類型的車輛,那么在使用userList.add()之前,至少要先行判斷,確保一輛車,只加入一個租用類型車數(shù)組中。如果判斷條件較多,建議使用switch,至少看起來會舒服很多。