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

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

提交作業,大家可以參考下,我覺得還能優化,望各位多提意見

package DDproject;


public abstract class car {? ? ?//汽車父類

public int id;

public String name;

? ? public int price;

? ? public int people;

? ? public int carry;

? ? public abstract void run();??

}

public class HuoCar extends car {? ?//貨車子類

public HuoCar(int id,String name,int price,int carry) {

this.id=id;

this.name=name;

this.price=price;

this.carry=carry;

}? ? ??

public void run() {

System.out.println(id+"\t"+name+"\t"+price+"元/天"+"\t"+"載貨:"+carry+"噸");

}


}

public class buscar extends car {? ? ? ? ?//載人子類

public buscar(int id,String name,int price,int people) {

this.id=id;

this.name=name;

this.price=price;

this.people=people;

}

public void run() {

System.out.println(id+"\t"+name+"\t"+price+"元/天"+"\t"+"載人:"+people+"人");

}

}

public class PKcar extends car {? ? //皮卡子類

public PKcar(int id,String name,int price,int people,int carry) {

this.id=id;

this.name=name;

this.price=price;

this.people=people;

this.carry=carry;

}

public void run() {

System.out.println(id+"\t"+name+"\t"+price+"元/天"+"\t"+"載人:"+people+"人"+"\t"+"載貨:"+carry+"噸");

}

}

package DDproject;


import java.util.Scanner;


public class Demotest {? ? ? //測試類

public void ZuChe() {? ? ? //是否進人租車系統方法

Scanner input = new Scanner(System.in);

try {

int select = input.nextInt();

if (select == 1) {

means();

} else if (select == 0) {

System.out.println("感謝您使用答答租車系統~再見!");

} else {

System.out.println("您輸入有誤,請重新輸入:");

System.out.println("您是否需要租車:1、是" + "\t" + "0、否");

ZuChe();

}

} catch (Exception e) {

System.out.println("您輸入有誤,請重新輸入:");

System.out.println("您是否需要租車:1、是" + "\t" + "0、否");

ZuChe();

}

}


public void means() {? ? ?//租車系統選車主方法

car che[] = { new buscar(1, "奧迪4A", 500, 4), new buscar(2, "馬自達6", 400, 4), new PKcar(3, "皮卡雪6", 450, 4, 2),

new buscar(4, "金龍", 800, 20), new HuoCar(5, "松花江", 400, 4), new HuoCar(6, "依維柯", 1000, 20) };

System.out.println("您可租車的類型及其價目表");

System.out.println("序號" + "\t" + "汽車名稱" + "\t" + "租金" + "\t" + "容量");

for (int i = 0; i < che.length; i++) {

che[i].run();

}

double money = 0, ton = 0;

int sum = 0;

System.out.println("請輸入您要汽車的數量:");

Scanner input = new Scanner(System.in);

int amount = input.nextInt();

int ID[] = new int[amount];

for (int i = 0; i < amount; i++) {

System.out.println("請輸入第" + (i + 1) + "輛車的序號");

int id = input.nextInt();

money = money + che[id - 1].price;

sum = sum + che[id - 1].people;

ton = ton + che[id - 1].carry;

ID[i] = id;

}

System.out.println("請輸入租車天數:");

int day = input.nextInt();

System.out.println("您的賬單:");

System.out.println("***可載人的車有:");

for (int i = 0; i < ID.length; i++) {

if (che[ID[i] - 1].people != 0) {

System.out.print(che[ID[i] - 1].name + "\t");

}

}

System.out.println("共載人:" + sum + "人");

System.out.println("***可載貨的車有:");

for (int i = 0; i < ID.length; i++) {

if (che[ID[i] - 1].carry != 0) {

System.out.print(che[ID[i] - 1].name + "\t");

}

}

System.out.println("共載貨:" + ton + "噸");

System.out.println("***租車總價格:" + money * day + "元");

}


public static void main(String[] args) {

System.out.println("歡迎您是使用答答租車系統:");

System.out.println("您是否需要租車:1、是" + "\t" + "0、否");

Demotest t = new Demotest();

t.ZuChe();

}

}


正在回答

4 回答

我還是有個問題啊 ,就是載人的車和載重的車就沒有區分呀?

載人的車打印的也是

System.out.print(che[ID[i] - 1].name + "\t");


載重的車打印也是

System.out.print(che[ID[i] - 1].name + "\t");



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

try-catch那段,else{} 可以省掉,或者將try-catch換成while測無限循環語句,并且用while的話能用continue而不用嵌套,try-catch用在這感覺不合適;

總體感覺挺簡潔到位的,贊?。

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

慕絲4409378 提問者

try-catch語句還是很有必要的,可以捕獲異常,捕獲輸入字符不匹配,萬一用戶輸入字母,運行就會中斷。else也是很有必要,萬一用戶輸入了比1還大的數字,運行也會中斷。
2019-07-20 回復 有任何疑惑可以回復我~
#2

慕絲4409378 提問者

用try-catch語句比較安全
2019-07-20 回復 有任何疑惑可以回復我~

請問那個means();是什么意思?

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

qq_慕沐4213791

不好意思 沒看到 現在看到了
2019-07-20 回復 有任何疑惑可以回復我~

你在父類里調用輸出函數就可以。不需要每個子類都寫

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

舉報

0/150
提交
取消

提交作業,大家可以參考下,我覺得還能優化,望各位多提意見

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

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

幫助反饋 APP下載

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

公眾號

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