亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

請問還能精簡嗎

car類:

package?com.CarRentSystem;

public?class?Vehicle?{

????String?name;
????int?carryPerson;
????int?carryFreight;
????int?price;

????public?Vehicle(String?name,int?carryPerson,int?carryFreight,int?price){
????????this.name?=?name;
????????this.carryPerson?=?carryPerson;
????????this.carryFreight?=?carryFreight;
????????this.price?=?price;
????}

????public?int?rent(int?days){
????????int?rent?=?days?*?this.price;
????????return?rent;
????}
}

主函數:

package?com.CarRentSystem;

import?java.util.Scanner;


public?class?Initial?{

????public?static?void?main(String[]?args){

????????Vehicle[]?cars?=?{new?Vehicle("奧迪A6",4,0,500),
??????????????????????????new?Vehicle("馬自達6",4,0,400),
??????????????????????????new?Vehicle("皮卡雪6",4,2,450),
??????????????????????????new?Vehicle("金龍",20,0,800),
??????????????????????????new?Vehicle("松花江",0,4,400),
??????????????????????????new?Vehicle("依維柯",0,20,1000)};

????????Scanner?input?=?new?Scanner(System.in);
????????System.out.println("歡迎使用喵喵租車系統!");
????????System.out.println("請問是否需要租車:1是,0否");
????????int?ifRent?=?Integer.parseInt(input.next());

????????if?(?ifRent?==?1){
????????????System.out.println("您可選擇的租車類型及價目表:");
????????????System.out.println("序號\t汽車名稱\t\t租金\t\t\t容量");
????????????for?(int?i?=?0?;i?<?6;i++){
????????????????System.out.print(i?+?1?+?"\t");
????????????????System.out.print(cars[i].name?+?"\t\t"?);
????????????????System.out.print(cars[i].price?+?"元/天\t\t");
????????????????if?(cars[i].carryPerson?==?0){
????????????????????System.out.println("載貨:"?+?cars[i].carryFreight?+?"噸");
????????????????}else?if?(cars[i].carryFreight?==?0){
????????????????????System.out.println("載人:"?+?cars[i].carryPerson?+?"人");
????????????????}else{
????????????????????System.out.println("載人:"?+?cars[i].carryPerson?+?"人,載貨:"?+?cars[i].carryFreight?+?"噸");
????????????????}
????????????}
????????}else{
????????????System.out.println("歡迎再次使用本系統!");
????????????return?;
????????}

????????System.out.println("請輸入需要租車的總天數:");
????????int?days?=?Integer.parseInt(input.next());

????????System.out.println("請輸入需要租車的總數量:");
????????int?carCount?=?Integer.parseInt(input.next());

????????int?allCars[]?=?new?int[carCount];
????????for?(int?j?=?1;j?<?carCount?+?1;j++){
????????????System.out.println("選擇第"?+?j?+?"輛車的編號:");
????????????allCars[j-1]?=?Integer.parseInt(input.next());
????????}

????????System.out.print("您選擇的車輛為:");

????????int?allPerson?=?0;
????????int?allFreight?=?0;
????????int?allPrice?=?0;

????????for?(int?m?=?0;?m?<?carCount;?m++){
????????????int?carCode?=?allCars[m];
????????????System.out.print(cars[carCode].name?+?"?");
????????????allPerson?+=?cars[carCode].carryPerson;
????????????allFreight?+=?cars[carCode].carryFreight;
????????????allPrice?+=?cars[carCode].rent(days);
????????}
????????System.out.println();
????????System.out.println("共可載人:"?+?allPerson?+?"人");
????????System.out.println("共可載貨:"?+?allFreight?+?"噸");
????????System.out.println("總價格為:"?+?allPrice?+?"元");
????}
}

輸出如下:

歡迎使用喵喵租車系統!

請問是否需要租車:1是,0否

1

您可選擇的租車類型及價目表:

序號 汽車名稱 租金 容量

1 奧迪A6 500元/天 載人:4人

2 馬自達6 400元/天 載人:4人

3 皮卡雪6 450元/天 載人:4人,載貨:2噸

4 金龍 800元/天 載人:20人

5 松花江 400元/天 載貨:4噸

6 依維柯 1000元/天 載貨:20噸

請輸入需要租車的總天數:

2

請輸入需要租車的總數量:

2

選擇第1輛車的編號:

2

選擇第2輛車的編號:

3

您選擇的車輛為:皮卡雪6 金龍?

共可載人:24人

共可載貨:2噸

總價格為:2500元


Process finished with exit code 0


正在回答

2 回答

你沒發現輸出結果與序號不匹配嗎?車輛序號都往后移了一位。

0 回復 有任何疑惑可以回復我~

從49行數組后面有點懵

0 回復 有任何疑惑可以回復我~
#1

qq__9440

把獲取到的值,存到數組里,然后循環遍歷數組下標輸出值。之后循環累加每個數值。最后輸出結果
2019-08-06 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

請問還能精簡嗎

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號