課程
/后端開發(fā)
/Java
/Java入門第二季 升級版
沒有什么思路,老師,能不能給一份完整的代碼作為參考呀。
2016-09-10
源自:Java入門第二季 升級版 12-1
正在回答
package?cc.candydoll.yun; import?java.util.Scanner; public?class?Initail?{ ?? ?? ?/** ??*?做一個答答租車系統(tǒng),顯示車的種類供用戶選擇 ??*?車的種類分為三種,載人、載貨、即可以載人又可以載貨 ??*?顯示出用戶選擇的車名,同時顯示出用戶選擇的車名 ??*?最后再計算總金額 ??*?@param?args ??*/ ?? ?static?Scanner?in=new?Scanner(System.in); ?static?int?sl;//保存用戶租車的? ?static?int?n; ?static?int?count=0;//統(tǒng)計輸入序號的次數(shù) ?static?int?d;//獲取天數(shù) ?static?int?allMoney=0;//統(tǒng)計總金額 ?static?int?allPeople=0;//統(tǒng)計總?cè)藬?shù) ?static?double?allGoods=0;//統(tǒng)計貨物重量 ?? ?public?static?void?main(String[]?args)?{ ??System.out.println("請問是否進(jìn)入答答租車系統(tǒng):1是?2否"); ??n=in.nextInt();//獲取用戶輸入的值來確認(rèn)是否進(jìn)入系統(tǒng) ??if(n==1){ ???Car?cars[]={new?People(1,"奧迪A4",500,4),//多態(tài)引用多個類?用數(shù)組遍歷多個對象 ????new?People(2,"馬自達(dá)6",400,4), ????new?Pika(3,"皮卡雪6",450,4,2), ????new?People(4,"金龍",800,20), ????new?Goods(5,"松花江",400,4), ????new?Goods(6,"依維柯",1000,20)}; ???System.out.println("請輸入租車的數(shù)量:"); ???sl=in.nextInt();//獲取用戶租車的數(shù)量 ???int[]?xh=new?int[sl];//通過數(shù)組保存輸入的序號 ???while(count<sl){ ????System.out.println("請輸入第"+(count+1)+"輛車的序號:"); ????int?x=in.nextInt();//輸入的序號 ????xh[count]=x;//將序號存儲在數(shù)組中 ????count++;//通過累加的形式,將序號一一存儲在數(shù)組中 ????continue; ???} ???System.out.println("請輸入租車天數(shù):"); ???d=in.nextInt(); ???//此部分計算 ???//計算人數(shù) ???for(int?i=0;i<cars.length;i++){//循環(huán)cars數(shù)組。 ????for(int?j=0;j<sl;j++){//循環(huán)用戶想租車的序號 ?????if(cars[i].no==xh[j]&&cars[i].people>0&&cars[i].good>0){//此處判定pika類型的車輛,計算結(jié)果 ??????System.out.print(cars[i].name+"?");//獲取pika車輛的名稱 ??????allGoods=allGoods+cars[i].good;//獲取pika類型車輛的貨物重量,進(jìn)行計算 ??????allPeople=allPeople+cars[i].people;//獲取pika類型的人數(shù),進(jìn)行計算 ??????allMoney=allMoney+cars[i].money;//獲取pika類型的租金,進(jìn)行計算 ?????} ????} ???} ???System.out.println("***載人的車:"); ???for(int?i=0;i<cars.length;i++){ ????for(int?j=0;j<sl;j++){ ?????if(cars[i].no==xh[j]&&cars[i].people>0){//此處判定載人類型的車輛,計算結(jié)果載人數(shù) ??????System.out.print(cars[i].name+"?"); ??????? ??????allPeople=allPeople+cars[i].people; ??????allMoney=allMoney+cars[i].money; ?????} ????} ???} ????System.out.println("總?cè)藬?shù):"+allPeople); ????System.out.println("***可載物的車有:"); ????for(int?i=0;i<cars.length;i++){//此處判定載貨類型的車輛,計算結(jié)果載物重量 ????for(int?j=0;j<sl;j++){ ?????if(cars[i].no==xh[j]&&cars[i].good>0){ ??????System.out.println("***載貨的車:"); ??????System.out.print(cars[i].name+"?"); ??????allGoods=allGoods+cars[i].good; ??????allMoney=allMoney+cars[i].money; ?????} ????} ???}? ???System.out.println("總載貨量:"+allGoods+"噸"); ???System.out.println("總金額"+allMoney*d); ??}else{ ???System.out.println("即將退出答答租車系統(tǒng)"); ??} ?} ?? }
慕碼人2389635
“多態(tài)引用多個類?用數(shù)組遍歷多個對象?!边@東西我們學(xué)過嗎?
package?cc.candydoll.yun; public?class?Pika?extends?Car?{ ?public?Pika(int?no,String?name,?int?money,int?people,double?good)?{ ??super(no,name,?money,people,good); ??//?TODO?Auto-generated?constructor?stub ??System.out.println(no+".?"+name+"?"+money+"元/天?"+people+"?"+good+"噸"); ?} }
package?cc.candydoll.yun; public?class?People?extends?Car?{ ?public?People(int?no,String?name,?int?money,int?people)?{ ??super(no,name,?money,people); ??//?TODO?Auto-generated?constructor?stub ??System.out.println(no+".?"+name+"?"+money+"元/天?"+people+"人"); ?} }
package?cc.candydoll.yun; public?class?Goods?extends?Car?{ ?double?good; ?public?Goods(int?no,String?name,?int?money,double?good)?{ ??super(no,name,?money,good); ??//?TODO?Auto-generated?constructor?stub ??setGood(good); ??System.out.println(no+".?"+name+"?"+money+"元/天?"+"?"+good+"噸"); ?} }
package?cc.candydoll.yun; class?Car?{ ?int?no; ?String?name; ?int?money; ?int?people; ?double?good; ?public?Car(int?no,String?name,int?money,double?good){ ??this.no=no; ??this.name=name; ??this.money=money; ??this.good=good; ?} ?public?Car(int?no,String?name,int?money,int?people){ ??this.no=no; ??this.name=name; ??this.money=money; ??this.people=people; ?} ?public?Car(int?no,String?name,int?money,int?people,double?good){ ??this.no=no; ??this.name=name; ??this.money=money; ??this.people=people; ??this.good=good; ?} ?public?String?getName()?{ ??return?name; ?} ?public?void?setName(String?name)?{ ??this.name?=?name; ?} ?public?int?getMoney()?{ ??return?money; ?} ?public?void?setMoney(int?money)?{ ??this.money?=?money; ?} ?public?int?getNo()?{ ??return?no; ?} ?public?void?setNo(int?no)?{ ??this.no?=?no; ?} ?public?int?getPeople()?{ ??return?people; ?} ?public?void?setPeople(int?people)?{ ??this.people?=?people; ?} ?public?double?getGood()?{ ??return?good; ?} ?public?void?setGood(double?good)?{ ??this.good?=?good; ?} ?? }
買本書仔細(xì)復(fù)習(xí)下
舉報
課程升級!以終為始告別枯燥,在開發(fā)和重構(gòu)中體會Java面向?qū)ο缶幊痰膴W妙
1 回答自己完成后~自己再看都覺得很頭痛。。。。。
3 回答請問下練習(xí)中的newScore,是什么意思?還有我自己直接調(diào)用的方法,得到分?jǐn)?shù)有什么不一樣?
1 回答看了別人的自己才有點思路,我的應(yīng)該是最簡單易懂的了,就是感覺沒用上第二季的知識
1 回答小白,自己寫的,渴望得到指導(dǎo)
2 回答自己做的,有不足之處還望指點
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2016-09-11
2016-10-21
“多態(tài)引用多個類?用數(shù)組遍歷多個對象?!边@東西我們學(xué)過嗎?
2016-09-11
2016-09-11
2016-09-11
2016-09-11
2016-09-10
買本書仔細(xì)復(fù)習(xí)下