Try   HackMD

程式設計 書櫃號:Java-2024-onExam

天空路1049號 之 地下圖書館
書架:112_1 Midterm NO_13
java exam if - else if

撰寫人KVJK_2125Wen, Nov 06, 2024 22:00(UTC+8)
考試日期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

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

解題

思路 Intuition/Approach

  • Step1.
    平日、假日、日間、夜間均使用字串判斷輸入,里程數設定為float。

  • Step2.
    設定一變數taxiM,用來存放轉換成公尺的里程數:
    float taxiM = taxiKm * 1000;

  • Step3.
    設定一變數total,存放總共的車資;
    設定一變數sthanplus,將會在程式碼中,作為計算多出單程的里程數。

  • Step4.
    程式碼中,首先判斷輸入1或是2(即平日或是假日)。
    這邊使用switch-case或是if都可。我一開始是使用switch-case,考試後半才更改成if

  • Step5.
    接著,判斷輸入3或是4(即日間或夜間)。
    同樣使用switch-case或是if都可,不過要小心自己被自己寫的程式碼混淆(that's me)。

  • Step6.
    判斷完1、2、3及4之後,接下來的邏輯都是一樣的。
    以1接三(即平日的日間)舉例來說:

    a. 如果里程數不超過1500公尺的話(即<=1500),總車資就是1500元。 超過則繼續判斷。
    b. 因為超過單程的里程數,因此車資可以先加單程的價格。
    c. 將sthan用來計算多出單程里程數多少。
    d. 若是多出的里程數不多於一次續程里程數(即<=300),將車資加上一次續程里程數的價錢;若是多出的里程數大於一次續程里程數,則將多餘的里程數乘上一次里程數的價錢,並加在車資上。

程式碼 Code(加註解)使用if

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

這個沒有做為考試答案上交。

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); } }