第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

哈哈~終于寫出來答答打車系統(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();
}
}

http://img1.sycdn.imooc.com//56a78a680001783c07710768.jpg

正在回答

3 回答

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] 就好了

0 回復(fù) 有任何疑惑可以回復(fù)我~

大哥,適當(dāng)?shù)厥褂?縮進 好不-_-!

0 回復(fù) 有任何疑惑可以回復(fù)我~

如果不想重復(fù)輸出不同類型的車輛,那么在使用userList.add()之前,至少要先行判斷,確保一輛車,只加入一個租用類型車數(shù)組中。如果判斷條件較多,建議使用switch,至少看起來會舒服很多。

0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

波浪波浪海海 提問者

可是這樣就沒辦法計算價錢和車輛數(shù)了哦。。。
2016-01-28 回復(fù) 有任何疑惑可以回復(fù)我~

舉報

0/150
提交
取消
Java入門第二季 升級版
  • 參與學(xué)習(xí)       531098    人
  • 解答問題       6280    個

課程升級!以終為始告別枯燥,在開發(fā)和重構(gòu)中體會Java面向?qū)ο缶幊痰膴W妙

進入課程

哈哈~終于寫出來答答打車系統(tǒng)啦,但會重復(fù)輸出載客車和載貨車,代碼如下,希望交流!

我要回答 關(guān)注問題
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號