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

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

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

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() {? ? ? //是否進(jìn)人租車系統(tǒng)方法

Scanner input = new Scanner(System.in);

try {

int select = input.nextInt();

if (select == 1) {

means();

} else if (select == 0) {

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

} 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() {? ? ?//租車系統(tǒng)選車主方法

car che[] = { new buscar(1, "奧迪4A", 500, 4), new buscar(2, "馬自達(dá)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("請輸入您要汽車的數(shù)量:");

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("請輸入租車天數(shù):");

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("歡迎您是使用答答租車系統(tǒng):");

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

Demotest t = new Demotest();

t.ZuChe();

}

}


正在回答

4 回答

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

載人的車打印的也是

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


載重的車打印也是

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



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

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

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

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

慕絲4409378 提問者

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

慕絲4409378 提問者

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

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

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

qq_慕沐4213791

不好意思 沒看到 現(xiàn)在看到了
2019-07-20 回復(fù) 有任何疑惑可以回復(fù)我~

你在父類里調(diào)用輸出函數(shù)就可以。不需要每個子類都寫

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

舉報

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

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

進(jìn)入課程

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

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

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

幫助反饋 APP下載

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

公眾號

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