熬了一天一夜做出來的,本人小白一個,剛學(xué)一星期,挺感興趣的,代碼有待改進(jìn)的地方還需要大神多指點
package?com.imooc; import?java.util.Scanner;???//添加Scanner工具類,這里主要用來接受并返回用戶輸入的值 public?class?InitialDada?{ public?static?void?main(String[]?args)?{ //?TODO?Auto-generated?method?stub System.out.println("歡迎使用答答租車系統(tǒng)\n您是否要租車:1、是\t0、?否"); //創(chuàng)建Scanner類的對像sc,用來接受用戶輸入的值 Scanner?sc?=?new?Scanner(System.in);? /* ?*?判斷用戶是否要租車,租車則通過循環(huán)語句輸出價目表 ?*?通過用戶輸入?yún)?shù)來調(diào)用方法獲取所需要的信息 ?*?統(tǒng)計信息然后輸出 ?*?nextInt();方法是返回用戶輸入值 ?*/ if(sc.nextInt()?==?1)?{ System.out.println("您可租車的類型及其價目表:\n序號\t汽車名稱\t"?+?"?????租金\t\t容量"); //創(chuàng)建所需對象 PassengerCar?pc?=?new?PassengerCar();????//創(chuàng)建客車對象 Truck?tc?=?new?Truck();??????????????????//創(chuàng)建貨車對象 PickupCar?pk?=?new?PickupCar();??????????//創(chuàng)建皮卡對象? //通過循環(huán)語句導(dǎo)入序號參數(shù)調(diào)用方法實現(xiàn)與相對應(yīng)的信息并輸出列表 for(int?j?=?1;j?<=?16;j++)?{ pc.pLoad(j); tc.tLoad(j); pk.tLoad(j);???????????????????????????????????? if(pc.number?==?j)?{???????????????????????????? System.out.println(j?+?"\t"?+?pc.name?+?"\t???"?+?pc.rent?+?"元/天\t載人:"?+?pc.pNumber?+?"人"); }? if(tc.number?==?j)?{ System.out.println(j?+?"\t"?+?tc.name?+?"\t???"?+?tc.rent?+?"元/天\t載貨:"?+?tc.dwtc?+?"噸"); }? if(pk.number?==?j){ System.out.println(j?+?"\t"?+?pk.name?+?"\t???"?+?pk.rent?+?"元/天\t載人:"?+?pk.pNumber?+?"人\t"?+?"載貨"?+?pk.dwtc); } } /* ?*? ?*/ System.out.println("請輸入你要租車的數(shù)量:"); int?s?=?sc.nextInt();????????? String?passengerCar?=?"\n";?????//定義要輸出載客的車變量 String?truck?=?"\n";????????????//定義要輸出載貨的車變量 int?headcount?=?0;??????????????//定義要輸出總載客量變量 float?totalLoad?=?0.0f;?????????//定義要輸出總載貨量變量 float?totalPrice?=?0.0f;????????//定義要輸出總金額變量 //通過用戶輸入租車的數(shù)量來實現(xiàn)選車的次數(shù) for(int?i?=?1;i?<=?s;i++)?{ System.out.println("請輸入第"?+?i?+?"輛車的序號"); do?{ int?c?=?sc.nextInt(); pc.name?=?null; tc.name?=?null; pk.name?=?null; //通過用戶所選序號參數(shù)調(diào)用方法獲取信息,實現(xiàn)總數(shù)計算 pc.pLoad(c); tc.tLoad(c); pk.tLoad(c); ????????????if(pc.name?!=?null)?{ ???????????? passengerCar?=?passengerCar?+?pc.name+"???"; ???????????? headcount?=?headcount?+?pc.pNumber; ???????????? totalPrice?=?totalPrice?+?pc.rent; ???????????? System.out.println("成功添加:"?+?pc.name); ???????????? break; ????????????} ????????????else?if(tc.name?!=?null)?{ ???????????? truck?=?truck?+?tc.name+"???"; ???????????? totalLoad?=?totalLoad?+?tc.dwtc; ???????????? totalPrice?=?totalPrice?+?tc.rent; ???????????? System.out.println("成功添加:"?+?tc.name); ???????????? break; ????????????}? ????????????else?if(pk.name?!=?null)?{ ???????????? passengerCar?=?passengerCar?+?pk.name+"???"; ???????????? truck?=?truck?+?pk.name+"\t"; ???????????? headcount?=?headcount?+?pk.pNumber; ???????????? totalLoad?=?totalLoad?+?pk.dwtc; ???????????? totalPrice?=?totalPrice?+?pk.rent; ???????????? System.out.println("成功添加:"?+?pk.name); ???????????? break; ????} ????????????//實現(xiàn)輸入有誤則通過循環(huán)返回重新輸入 ????????????else?{ ???????????? System.out.println("您的輸入有誤,沒有找到您所需車型\n請重新輸入第"?+?i?+?"輛車的序號"); ????????????} }?while(1?>?0); } /* ?*? ?*/ System.out.println("請輸入租車天數(shù):"); int?day?=?sc.nextInt(); totalPrice?=?totalPrice?*?day; /* ?*? ?*/ System.out.println("你的賬單:"); System.out.println("********載人的車有:"?+?passengerCar?+?"\n共載人:"?+?headcount?+?"人"); System.out.println("********載貨的車有:"?+?truck?+?"\n共載貨:"?+?totalLoad?+?"噸"); System.out.println("********租車天數(shù):"?+?day?+?"天"?+?"\n********租車總金額:"?+?totalPrice?+?"元"); } /* ?*?如果用戶最上面選擇否,這直接跳到這一部 ?*/ System.out.println("********感謝您的光臨,再見!*********"); } }
package?com.imooc;????? public?class?PassengerCar{ //定義一個客車的類 public?int?number;??????????//定義序號 public?String?name;?????????//定義名稱 public?float?rent;??????????//定義租金 public?int?pNumber;?????????//定義載客量 //定義載客帶參的方法 public?void?pLoad(int?number)?{ //if語句用來判斷接受序號的參數(shù),從而給客車屬性賦值 if(number?==?1)?{ this.number?=?number; this.name?=?"奧迪A4"; this.rent?=?500.0f; this.pNumber?=?4; } if(number?==?2)?{ this.number?=?number; this.name?=?"馬自達(dá)6"; this.rent?=?400.0f; this.pNumber?=?4; } if(number?==?4)?{ this.number?=?number; this.name?=?"金龍"; this.rent?=?800.0f; this.pNumber?=?20; } if(number?==?7)?{ this.number?=?number; this.name?=?"奔馳s600"; this.rent?=?800.0f; this.pNumber?=?5; } if(number?==?8)?{ this.number?=?number; this.name?=?"寶馬750i"; this.rent?=?800.0f; this.pNumber?=?5; } if(number?==?9)?{ this.number?=?number; this.name?=?"勞斯萊斯"; this.rent?=?1500.0f; this.pNumber?=?5; } if(number?==?10)?{ this.number?=?number; this.name?=?"賓利"; this.rent?=?1500.0f; this.pNumber?=?5; } if(number?==?11)?{ this.number?=?number; this.name?=?"大眾"; this.rent?=?500.0f; this.pNumber?=?5; } if(number?==?12)?{ this.number?=?number; this.name?=?"保時捷"; this.rent?=?600.0f; this.pNumber?=?5; } if(number?==?13)?{ this.number?=?number; this.name?=?"豐田"; this.rent?=?400.0f; this.pNumber?=?5; } if(number?==?14)?{ this.number?=?number; this.name?=?"雷克薩斯"; this.rent?=?500.0f; this.pNumber?=?5; } if(number?==?15)?{ this.number?=?number; this.name?=?"華晨金杯"; this.rent?=?400.0f; this.pNumber?=?9; } } }
package?com.imooc; public?class?Truck?{ //定義一個卡車的類 public?int?number;??????????//定義序號 public?String?name;?????????//定義名稱 public?float?rent;??????????//定義租金 public?float?dwtc;??????????//定義載貨量 //定義載貨帶參的方法 public?void?tLoad(int?number)?{ //if語句用來判斷接受序號的參數(shù),從而給貨車屬性賦值 if(number?==?5)?{ this.number?=?number; this.name?=?"松花江"; this.rent?=?400.0f; this.dwtc?=?4.0f; } if(number?==?6)?{ this.number?=?number; this.name?=?"依維柯"; this.rent?=?1000.0f; this.dwtc?=?20.0f; } if(number?==?16)?{ this.number?=?number; this.name?=?"福田"; this.rent?=?1000.0f; this.dwtc?=?20.0f; } } }
package?com.imooc; public?class?PickupCar?extends?Truck{ //定義一個皮卡的類,并且繼承父類Truck(卡車類) public?int?pNumber;???????????//定義載客量,父類沒有的屬性 //修改父類載重的方法,添加給載客量賦值 public?void?tLoad(int?number)?{ //if語句用來判斷接受序號的參數(shù),從而給皮卡屬性賦值 if(number?==?3)?{ this.number?=?number; this.name?=?"皮卡雪6"; this.rent?=?450.0f; this.dwtc?=?2.0f; this.pNumber?=?4; } } }
基本上都是用前兩季的方法,就是Scanner工具類獲取參數(shù)的方法是我百度學(xué)的,因為前兩季沒有講怎么獲取用戶參數(shù),有需要改進(jìn)的地方,大神們多多指教,在imooc共同成長
2015-01-13
已經(jīng)很好啦,看得很清晰