# 程式設計 書櫃號:Java-2024-onExam >天空路1049號 之 地下圖書館 >書架:112_1 Midterm `NO_13` > `java` `exam` `if - else if` > 撰寫人[name=KVJK_2125] [time=Wen, Nov 06, 2024 22:00](UTC+8) > 考試日期[time=Wen, Nov 06, 2024 13:30](UTC+8) ## 題目 ### 描述 Description 以下兩表的計算方式,輸入平日(1)或假日(2),再輸入白天(3)或夜間(4)後,接著輸入里程數,並計算出計程車車資。 | 平日 | 距離 | 價錢 | | -------- | -------- | ---- | | 白天單程 | 1500公尺 | 70元 | | 白天續程 | 300公尺 | 5元 | | 夜間單程 | 1250公尺 | 70元 | | 夜間續程 | 250公尺 | 5元 | | 假日 | 距離 | 價錢 | | -------- | -------- | ---- | | 白天單程 | 1500公尺 | 80元 | | 白天續程 | 300公尺 | 10元 | | 夜間單程 | 1250公尺 | 80元 | | 夜間續程 | 250公尺 | 10元 | ### 範例 Example    ## 解題 ### 思路 Intuition/Approach - Step1.<br> 平日、假日、日間、夜間均使用字串判斷輸入,里程數設定為float。<br> - Step2.<br> 設定一變數`taxiM`,用來存放轉換成公尺的里程數: `float taxiM = taxiKm * 1000;`<br> - Step3.<br> 設定一變數`total`,存放總共的車資;<br>設定一變數`sthan`及`plus`,將會在程式碼中,作為計算多出單程的里程數。 - Step4.<br> 程式碼中,首先判斷輸入1或是2(即平日或是假日)。<br>這邊使用`switch-case`或是`if`都可。我一開始是使用`switch-case`,考試後半才更改成`if`。 - Step5.<br> 接著,判斷輸入3或是4(即日間或夜間)。<br>同樣使用`switch-case`或是`if`都可,不過要小心自己被自己寫的程式碼混淆(that's me)。 - Step6.<br> 判斷完1、2、3及4之後,接下來的邏輯都是一樣的。<br>以1接三(即平日的日間)舉例來說:<br> -- a. 如果里程數不超過1500公尺的話(即`<=1500`),總車資就是1500元。 超過則繼續判斷。 -- b. 因為超過單程的里程數,因此車資可以先加單程的價格。 -- c. 將`sthan`用來計算多出單程里程數多少。 -- d. 若是多出的里程數不多於一次續程里程數(即`<=300`),將車資加上一次續程里程數的價錢;若是多出的里程數大於一次續程里程數,則將多餘的里程數乘上一次里程數的價錢,並加在車資上。 ### 程式碼 Code(加註解)使用if ``` clink= import java.util.Scanner; public class NO_13 { public static void main(String[] args) { Scanner scn = new Scanner(System.in); //input taxiDay平日1, 假日2 System.out.println("請輸入平日(1)或假日(2):"); String taxiDay = scn.nextLine(); //input taxiTime日間(3), 夜間(4) System.out.println("請輸入日間(3)或夜間(4):"); String taxiTime = scn.next(); //input taxiKm(公里) System.out.println("請輸入里程(公里)數[小數點可精確至公尺]:"); float taxiKm = scn.nextFloat(); //轉換成公尺 float taxiM = taxiKm * 1000; //總共多少錢 int total = 0; //多多長 float sthan = 0,plus = 0; //平日 if(taxiDay.equals("1")) { //日間 if(taxiTime.equals("3")) { //距離小於1500的話 if(taxiM <= 1500.0) { total += 70; } //距離大於1500的話 else if (taxiM > 1500.0) { total += 70; //得知多多少距離 sthan = taxiM - 1500; //多的距離不多於300 if(sthan <= 300) total += 5; //多的距離多餘300 else { //如果沒有轉乘int會發生多計算的問題,因此在這裡將多餘的公尺數換成int plus = (int)sthan / 300; //將多餘的公尺數乘上累加的價錢 total += plus * 5; if(sthan % 300 > 0) total += 5; } } } //夜間 if(taxiTime.equals("4")) { //距離不多於1250的話 if(taxiM <= 1250) total += 70; //距離多餘1250 else if (taxiM > 1250) { total += 70; //得知多多少距離 sthan = taxiM - 1250; //多的距離不多於250的話 if(sthan <= 250) total += 5; //多的距離多餘250 else { //如果沒有轉乘int會發生多計算的問題,因此在這裡將多餘的公尺數換成int plus = (int)sthan / 250; //將多餘的公尺數乘上累加的價錢 total += plus * 5; if(sthan % 250 > 0) total += 5; } } } } //假日 if(taxiDay.equals("2")) { if(taxiTime.equals("3")) { if(taxiM <= 1500) total += 80; else if (taxiM > 1500) { total += 80; sthan = taxiM - 1500; if(sthan <= 300) total += 10; else { plus = (int)sthan / 300; total += plus * 10; if(sthan % 300 > 0) total += 10; } } } if(taxiTime.equals("4")) { if(taxiM <= 1250) total += 80; else if (taxiM > 1250) { total += 80; sthan = taxiM - 1250; if(sthan <= 250) total += 10; else { plus = (int)sthan / 250; total += plus * 10; if(sthan % 250 > 0) total += 10; } } } } System.out.println("車資:" + total); } } ``` #### 錯誤更正 Debug - `.equals()`:現在程式碼中可以看到,`taxiDay.equals("1")`來判斷字串是否相符。其實原本只是單純的使用`==`來判斷,但是因為沒辦法判斷,因此在考試中途選擇換成`.equals()`。 - `plus = (int)sthan / 250`:原本並沒有用`(int)`,但是小數點後的數字會影響到下一行的乘法結果,於是在此加上`(int)``。 ### 程式碼 Code(加註解) 使用switch-case 這個沒有做為考試答案上交。 ``` clink= import java.util.Scanner; public class NO_13 { public static void main(String[] args) { Scanner scn = new Scanner(System.in); //input taxiDay平日1, 假日2 System.out.println("請輸入平日(1)或假日(2):"); String taxiDay = scn.nextLine(); //input taxiTime日間(3), 夜間(4) System.out.println("請輸入日間(3)或夜間(4):"); String taxiTime = scn.next(); //input taxiKm(公里) System.out.println("請輸入里程(公里)數[小數點可精確至公尺]:"); float taxiKm = scn.nextFloat(); //轉換成公尺 float taxiM = taxiKm * 1000; //總共多少錢 int total = 0; //多多長 float sthan = 0,plus = 0; switch(taxiDay){ //平日 case "1": //日間 switch(taxiTime){ case "3": //距離小於1500的話 if(taxiM <= 1500.0) { total += 70; } //距離大於1500的話 else if (taxiM > 1500.0) { total += 70; //得知多多少距離 sthan = taxiM - 1500; //多的距離不多於300 if(sthan <= 300) total += 5; //多的距離多餘300 else { //如果沒有轉乘int會發生多計算的問題,因此在這裡將多餘的公尺數換成int plus = (int)sthan / 300; //將多餘的公尺數乘上累加的價錢 total += plus * 5; if(sthan % 300 > 0) total += 5; } } break; case "4": //距離不多於1250的話 if(taxiM <= 1250) total += 70; //距離多餘1250 else if (taxiM > 1250) { total += 70; //得知多多少距離 sthan = taxiM - 1250; //多的距離不多於250的話 if(sthan <= 250) total += 5; //多的距離多餘250 else { //如果沒有轉乘int會發生多計算的問題,因此在這裡將多餘的公尺數換成int plus = (int)sthan / 250; //將多餘的公尺數乘上累加的價錢 total += plus * 5; if(sthan % 250 > 0) total += 5; } } break; } break; //假日 case "2": switch(taxiTime){ case "3": if(taxiM <= 1500) total += 80; else if (taxiM > 1500) { total += 80; sthan = taxiM - 1500; if(sthan <= 300) total += 10; else { plus = (int)sthan / 300; total += plus * 10; if(sthan % 300 > 0) total += 10; } } break; case "4": if(taxiM <= 1250) total += 80; else if (taxiM > 1250) { total += 80; sthan = taxiM - 1250; if(sthan <= 250) total += 10; else { plus = (int)sthan / 250; total += plus * 10; if(sthan % 250 > 0) total += 10; } } break; } break; } System.out.println("車資:" + total); } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up