java學習記錄

通過半個月的java語言學習,完成一個簡單的滴滴租車小demo,都是自己手動輸入,跟參考代碼的答案基本不一樣,但實現思路基本一致;

 1 package project9;
 2 import java.util.Scanner;
 3 // 測試類
 4 public class testClass9 {
 5     public static void main(String[] args){
 6         Car[] car ={new LightCar(),new HeavyCar(),new LightPassengerCar(),new HeavyPassengerCar(),new PikaCar()};
 7         System.out.println("歡迎使用答答租車系統:");
 8         System.out.println("您是否要租車:1是 0否");
 9         Scanner input = new Scanner(System.in);
10         int isNeedCar = input.nextInt(); //是否要租車
11         if(isNeedCar ==0){
12             System.out.println("感謝您的使用,歡迎再次光臨!");
13         }else if(isNeedCar !=0 && isNeedCar!=1){
14             System.out.println("您的輸入有誤,請輸入0或者1!");
15         }else if(isNeedCar ==1){
16             System.out.println("請輸入您要租汽車的數量");
17             int needCarNum = input.nextInt();
18             if(needCarNum>car.length){
19                 System.out.println("您要租汽車的數量大於本店汽車的數量,請重新輸入要租的汽車的數量!");
20             }else{
21                 int[] saveSelectedCarNo = new int[needCarNum];//存儲被客服選擇組的汽車的下標值集合
22                 for(int i=1;i<=needCarNum;i++){
23                     System.out.println("請輸入第"+i+"輛車的序號:");
24                     int whichCarNo = input.nextInt(); //某個車的下標值
25                     saveSelectedCarNo[i-1] =whichCarNo;
26                     }
27                 System.out.println("您輸入租車天數:");
28                 int needCarDay = input.nextInt(); //租車天數
29                 System.out.println("您的租車賬單:");
30                 System.out.println("***可載人的車有:");
31                 int canManned = 0; //可載人的數量總和,初始值設爲0
32                 double canCarryCargo = 0; //可載貨物的數量總和,初始值設爲0
33                 double money =0 ; //租車總價格,初始值設爲0
34                 for(int j=0;j<saveSelectedCarNo.length;j++){
35                     if(car[saveSelectedCarNo[j]].getCanManned() == true){
36                       System.out.print(car[saveSelectedCarNo[j]].getCarName()+"\t");
37                       canManned = canManned + car[saveSelectedCarNo[j]].getNumberOfPeople();
38                       money = money + car[saveSelectedCarNo[j]].getMoney();
39                     }
40                 }
41                 System.out.println("共載人:"+canManned+"人");
42                 System.out.println("***可載貨的車有:");
43                 for(int k=0;k<saveSelectedCarNo.length;k++){
44                     if(car[saveSelectedCarNo[k]].getcanCarryCargo() == true){
45                         System.out.print(car[saveSelectedCarNo[k]].getCarName()+"\t");
46                         canCarryCargo = canCarryCargo + car[saveSelectedCarNo[k]].getVolumeOfCargo();
47                         money = money + car[saveSelectedCarNo[k]].getMoney();
48                     }
49                 }
50                 System.out.println("共載貨:"+canCarryCargo+"噸");
51                 System.out.println("租車總價格:"+money*needCarDay+"元");
52 
53                 }
54             }
55         }
56 
57     }

 實現效果如下:

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章