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

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

自己寫的租車程序,感覺前面很多知識沒有用到,請指正

汽車父類

package com.imooc;
public class Car {
?public String name;
?public int price;
?public int ton;
?public int people;
?public void car(String name1,int price1,int ton1,int peple1){
??name=name1;
??price=price1;
??ton=ton1;
??people=peple1;
?}

}

測試程序

package com.imooc;
import java.util.Scanner;
public class Initial extends Car {
?
?public static void main(String[] args) {
??// TODO Auto-generated method stub
??//初始化車輛信息
??int carNum=8;
??Car car0[]=new Car[carNum];
??for(int i=0;i<car0.length;i++){
?? ??car0[i]=new Car();
?? ?}
???? car0[0].car("奧迪A4", 500, 0, 4);
???? car0[1].car("馬自達6", 400, 0, 4);
???? car0[2].car("皮卡雪6", 450, 2, 4);
???? car0[3].car("金龍?????????????? ", 800, 0, 20);
???? car0[4].car("松花江?????????? ", 400, 4, 0);
???? car0[5].car("依維柯?????????? ", 1000, 20, 0);
???? car0[6].car("polo ", 300,? 0, 4);
???? car0[7].car("桑塔納?????????? ", 350, 0, 4);
???? System.out.println("歡迎使用滴滴租車系統!"+"\n"+"您是否要租車:1是 0否");
??Scanner str=new Scanner(System.in);
???????
??????? //顯示所有車輛信息
??????? if (str.nextInt()==1) {
??????? ?//調用顯示車輛信息的方法
??????? ?displayInfo(carNum,car0);
??????? }else {
??????? ?//調用退出方法。
??????? ? exit();
??????? }
??????? //輸入租用的汽車數量
??????? System.out.println("請輸入您要租的汽車數量:");
??????? Scanner str1=new Scanner(System.in);
???????
??????? //用數組num 存儲選擇車的序號
??????? int n=str1.nextInt();
??????? int[] num=new int[n];
??????? for(int i=1;i<=n;i++){
??????? ?System.out.println("請輸入第"+i+"輛車的序號:");
??????? ?Scanner str2=new Scanner(System.in);
??????? ?num[i-1]=str2.nextInt();
??????? }
???????
??????? //輸入租的天數
??????? System.out.println("請輸入您要租車的天數:");
??????? Scanner str11=new Scanner(System.in);
??????? int days=str11.nextInt();
??????? //計算賬單
??????? calculate(days,num,car0);
?}
?
?//顯示車輛信息方法
??? public static void displayInfo(int num, Car car[]){
??? ?int i =num;
??? ?int sn=0;
??? ?String[] header=new String[]{"序號??? "??? ,"汽車名稱????? ","租金???????????? ","容量"};
??? ?for(int n=0;n<header.length;n++){
??? ??System.out.print(header[n]+"?? ");
??? ?}
??? ?System.out.print("\n");
??? ?for(int j=0; j<i;j++ ){
??? ??sn=j+1;
??? ??System.out.println(sn+".?? "+car[j].name+"?? "+car[j].price+"/天"+"???? "+"載人:"+
??? ??car[j].people+"人"+" "+"載貨:"+car[j].ton+"噸");
??? ?}
??? }
?
??? //計算統計車型,可載人數,噸位,價格
??? public static void calculate(int days,int num[],Car car[]){
??? ?int people=0;
??? ?float price=0.0f;
??? ?int ton=0;
??? ?String[] peopleCar=new String[num.length]; //存儲載人車輛名稱
??? ?String[] goodsCar=new String[num.length];? //存儲載貨車輛名稱
??? ?for (int i=0; i<num.length;i++){
??? ??if(car[num[i]-1].people>0){
??? ???peopleCar[i]=car[num[i]-1].name;
??? ???people+=car[num[i]-1].people;
??? ??}
??? ??if(car[num[i]-1].ton>0){
??? ???goodsCar[i]=car[num[i]-1].name;
??? ???ton+=car[num[i]-1].ton;
??? ??}
??? ??price=price+car[num[i]-1].price;
??? ?}
??? ?
??? ?System.out.println("您的賬單:"+"\n"+"***可載人的車有:");
??? ?for(int i=0;i<peopleCar.length;i++){
??? ??if (peopleCar[i]!=null){
??? ??System.out.print(peopleCar[i]+"??? ");
??? ??}
??? ?}
??? ?System.out.println("共載人:"+people+"人");
??? ?System.out.println("***載貨的車有:");
??? ?for(int i=0;i<goodsCar.length;i++){
??? ??if (goodsCar[i]!=null){
??? ??System.out.print(goodsCar[i]+"??? ");
??? ??}
??? ?}
??? ?System.out.println("共載貨:"+ton+"噸");
??? ?System.out.println("***租車的總價格:"+price*days+"元");
??? ?
??? }
??? //結束選車
??? public static void exit(){
??? ?System.out.println("歡迎下次光臨滴滴租車系統,謝謝!");
??? }
}


正在回答

1 回答

666

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

舉報

0/150
提交
取消
Java入門第二季 升級版
  • 參與學習       531142    人
  • 解答問題       6326    個

課程升級!以終為始告別枯燥,在開發和重構中體會Java面向對象編程的奧妙

進入課程

自己寫的租車程序,感覺前面很多知識沒有用到,請指正

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

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

幫助反饋 APP下載

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

公眾號

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