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

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

來交作業(yè)了!備注了知識點(diǎn)

package?com.stduy;
import??java.util.Scanner;
//從功能上分,總共分為三種,載人汽車,載貨汽車,既載人又載貨。
//對應(yīng)Bus,Truck,PickUp
public?class?Initail?{
????private?static?int?choice;
????static?Scanner??input?=?new?Scanner(System.in);
????private?static?Car[]?cars=?new?Car[]{
????????????new?Bus("巴士車",500,10),
????????????new?Truck("卡車",300,5),
????????????new?PickUp("皮卡車",?600,?6,?7)
????};
????public?static?void?main(String[]?args)?{
????????System.out.println("歡迎使用租車系統(tǒng)");
????????//Java不需要先聲明,和C不同。
????????//?原因是:類加載編譯時(shí)就把程序的入口初始化了,執(zhí)行時(shí)是JAVA虛擬機(jī)所識別的特定字節(jié)碼,
????????//?C卻是處理器可直接執(zhí)行的二進(jìn)制指令,編譯的原理不一樣,語言獨(dú)特的特性與運(yùn)行環(huán)境不一樣。
????????IsNeed();//判斷是否要租車
????????if(choice==1){
????????????DisplayList();?//顯示可租車清單
????????????int?num=RentNum();//獲取租車的種類數(shù)
????????????RentList[]?rentLists=new?RentList[num];//填寫出租信息表
????????????rentLists=RentForm(num);
????????????Pay(rentLists,cars);//生成賬單
????????}
????}

????public?static?void?IsNeed()?{
????????System.out.println("您是否要租車:1是?0否");
????????choice?=?input.nextInt();
????????if?(choice?==?0)?{
????????????System.out.println("感謝使用,再見");
????????}?else?if?(choice?!=?1)?{
????????????System.out.println("輸入錯(cuò)誤,請重新輸入");
????????}
????}

????//關(guān)鍵字instanceof,測試一個(gè)對象是否為一個(gè)類的實(shí)例
????public?static?void?DisplayList(){
????????System.out.println("您可租車的型號和價(jià)格表");
????????for(int?i?=?0;?i<cars.length;?i++){
????????????????if(cars[i]?instanceof?Bus){
????????????????????Bus?car=(Bus)cars[i];//強(qiáng)制類型轉(zhuǎn)換
????????????????????System.out.println((i?+?1)?+?"?"?+?car.name+"?"+car.rent+"元/天"+"?"+"核載"+car.person+"人");
????????????????}
????????????????else?if(cars[i]?instanceof?Truck){
????????????????????Truck?car=(Truck)cars[i];
????????????????????System.out.println((i?+?1)?+?"?"?+?car.name+"?"+car.rent+"元/天"+"?"+"核載"+car.goods+"噸");
????????????????}
????????????????else?if(cars[i]?instanceof?PickUp){
????????????????????PickUp?car=(PickUp)cars[i];
????????????????????System.out.println((i?+?1)?+?"?"?+?car.name+"?"+car.rent+"元/天"+"?"+"核載"+car.person+"人"+"?"+"核載"+car.goods+"噸");
????????????????}
????????????}
????}
????//以下為輸入部分
????//獲取租車數(shù)量
????private?static?int?RentNum(){
????????System.out.println("請輸入您要總共租車的種類數(shù):");
????????int?RentNum=input.nextInt();
????????return?RentNum;
????}
????//租車信息表
????private??static??RentList[]?RentForm??(int?num){
????????//租車信息對象數(shù)組
????????RentList[]??rentLists=new?RentList[num];

????????for(int?i=0;i<num;i++){
????????????//型號
????????????System.out.println("請輸入租的第"+(i+1)+"類車的信息");
????????????System.out.print("型號:");
????????????int?Model=input.nextInt();
????????????//該型號的數(shù)量
????????????System.out.print("數(shù)量:");
????????????int?Num=input.nextInt();
????????????//該型號租的天數(shù)
????????????System.out.print("天數(shù):");
????????????int?Day=input.nextInt();
????????????//將信息存入對象數(shù)組
????????????rentLists[i]=new?RentList();//這步不能掉
????????????rentLists[i].setModel(Model);
????????????rentLists[i].setNum(Num);
????????????rentLists[i].setDay(Day);
????????}
????????return?rentLists;
????}
????//生成賬單
????private?static?void?Pay(RentList[]?rentLists,Car[]?cars){
????????//打印清單
????????System.out.println("************結(jié)算清單*************");
????????System.out.println("型號????????數(shù)量????????租用天數(shù)");
????????double?SumMoney=0;
????????for(int?i=0;i<rentLists.length;i++){
????????????//用get方法獲得數(shù)據(jù),更好的封裝RentList類
????????????int?Model=rentLists[i].getModel();
????????????int?Num=rentLists[i].getNum();
????????????int?Day=rentLists[i].getDay();
????????????double?Rent=cars[i].getRent();
????????????SumMoney+=Num*Day*Rent;
????????????System.out.println(Model+"????????????"+Num+"????????????"+Day);
????????}
????????System.out.println("您總共要支付"+SumMoney+"元");
????}
????//這里也可以通過重寫toString方法輸出
????//toString方法為Object類中的方法,返回對象的哈希碼,即地址
????//不重寫toString方法,則直接輸入對象是輸出該對象的地址
}

//父類,車

package?com.stduy;
//車
public?class?Car?{
????protected???String?name;//車型,protect方便繼承
????protected???double?rent;//租金
????public?Car(String?name,double?rent){//用構(gòu)造方法初始化
????????this.name=name;
????????this.rent=rent;
????}

????public?String?getName()?{
????????return?name;
????}
????public?double?getRent()?{
????????return?rent;
????}

????public?void?setName(String?name)?{
????????this.name?=?name;
????}

????public?void?setRent(double?rent)?{
????????this.rent?=?rent;
????}
}

//載人車

package?com.stduy;

//Bus,載人汽車
public?class?Bus?extends?Car?{
????//子類的構(gòu)造過程當(dāng)中必須調(diào)用其父類的構(gòu)造方法
????//無參時(shí)隱式執(zhí)行super(),子類如果沒有顯式調(diào)用,父類又沒有無參的構(gòu)造方法則編譯出錯(cuò)
????//java里一個(gè)類可以有多個(gè)構(gòu)造方法
????//super關(guān)鍵字必須放在第一行
????protected?int?person;//載人數(shù)
????//構(gòu)造方法
????public?Bus(String?name,?double?rent,int?person)?{
????????super(name,?rent);
????????this.person=person;
????}

????public?int?getPerson()?{
????????return?person;
????}

????public?void?setPerson(int?person)?{
????????this.person?=?person;
????}
}

//載貨車

package?com.stduy;
//貨車
public?class?Truck?extends?Car{

????protected?double?goods;//貨物
????//構(gòu)造方法
????public?Truck(String?name,?double?rent,?double?goods)?{
????????super(name,?rent);
????????this.goods=goods;
????}

????public?double?getGoods()?{
????????return?goods;
????}

????public?void?setGoods(double?goods)?{
????????this.goods?=?goods;
????}
}

//既載貨又載人

package?com.stduy;

//皮卡,既能載貨又能載人
public?class?PickUp?extends?Car?{

????protected?int?person;//載人數(shù)
????protected?double?goods;//貨物
????//構(gòu)造方法
????public?PickUp(String?name,?double?rent,int?person,double?goods)?{
????????super(name,?rent);
????????this.person=person;
????????this.goods=goods;
????}

????public?int?getPerson()?{
????????return?person;
????}

????public?double?getGoods()?{
????????return?goods;
????}

????public?void?setPerson(int?person)?{
????????this.person?=?person;
????}

????public?void?setGoods(double?goods)?{
????????this.goods?=?goods;
????}
}

//租車信息表

package?com.stduy;
//租車信息表
public?class?RentList?{
????private??int?Model;//型號
????private??int?Num;//要租該型號的數(shù)量
????private??int?Day;//要租該型號的天數(shù)

????public?int?getModel()?{
????????return?Model;
????}

????public?int?getNum()?{
????????return?Num;
????}

????public?int?getDay()?{
????????return?Day;
????}

????public?void?setModel(int?model)?{
????????Model?=?model;
????}

????public?void?setNum(int?num)?{
????????Num?=?num;
????}

????public?void?setDay(int?day)?{
????????Day?=?day;
????}
}


正在回答

6 回答

膜拜大佬,大佬學(xué)習(xí)多久了

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

遠(yuǎn)赴山河萬里 提問者

兩天...
2020-04-15 回復(fù) 有任何疑惑可以回復(fù)我~

為啥我自己就寫不出來呢??難過


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

大神厲害!我自己按照你的這個(gè)寫了一遍,發(fā)現(xiàn)了一個(gè)bug;如果只租兩種或一種車,結(jié)果就會(huì)出現(xiàn)錯(cuò)誤。建議把double Rent = cars[i].getRent();改成double Rent = cars[Model-1].getRent();


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

遠(yuǎn)赴山河萬里 提問者

對,這里是有問題,感謝指正,我也是才學(xué)java,寫C的習(xí)慣總是改不掉,所以才備注知識點(diǎn)。
2020-03-20 回復(fù) 有任何疑惑可以回復(fù)我~
#2

遠(yuǎn)赴山河萬里 提問者

這里的算法其實(shí)可以有很多種實(shí)現(xiàn),主要是習(xí)慣語法,感謝指正
2020-03-20 回復(fù) 有任何疑惑可以回復(fù)我~

你好,想問個(gè)問題,你的Pay方法中,rentList 和 cars的型號沒有判斷是否匹配,如果用戶不按照順序選擇車的類型,車的租金應(yīng)該會(huì)匹配有誤吧

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

遠(yuǎn)赴山河萬里 提問者

對,這里是有問題,我寫完也發(fā)覺了,但是后來我又想實(shí)現(xiàn)刪除等功能,我就用鏈表實(shí)現(xiàn)了,這個(gè)就被推翻了,所以就沒改。望見諒,只是提供一個(gè)框架,里面的數(shù)據(jù)結(jié)構(gòu)可以自己設(shè)計(jì),才疏學(xué)淺,抱歉。
2020-03-19 回復(fù) 有任何疑惑可以回復(fù)我~
#2

慕圣大越越

把double Rent = cars[i].getRent();改成double Rent = cars[Model-1].getRent();就匹配嘍
2020-03-19 回復(fù) 有任何疑惑可以回復(fù)我~

寫掉了要算的總核載量和總核載人數(shù),不過大同小異,就那里加幾行

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

http://img1.sycdn.imooc.com//5e70def00001061819201030.jpg

附一下運(yùn)行結(jié)果

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

慕雪5409074

你的這個(gè)繼承寫的不錯(cuò),思路明確,很多人寫的繼承,子類啥事都不做,全讓父類一個(gè)人做了,把父類當(dāng)成全能車。
2020-04-15 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

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

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

進(jìn)入課程

來交作業(yè)了!備注了知識點(diǎn)

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

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

幫助反饋 APP下載

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

公眾號

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