- public class JPA05 {
- public static void main(String[] argv) {
- int hours = 0; // 停車時數
- hours = 2;
- park(hours);
- System.out.println("--------------------");
- hours = 3;
- park(hours);
- System.out.println("--------------------");
- hours = 5;
- park(hours);
- System.out.println("--------------------");
- hours = 8;
- park(hours);
- }
- public static void park(int hours) {
- int[] hourTable = { 0, 2, 4, 6 }; // 時段
- int[] feeTable = { 30, 50, 80, 100 }; // 時段費率
- int fee = 0; // 停車費用
- if (hours >= 1 && hours <= 2) {
- fee=hours*feeTable[0];
- }
- if (hours >= 3 && hours <= 4) {
- fee=(hours-hourTable[1])*feeTable[1]+2*feeTable[0];
- }
- if (hours >= 5 && hours <= 6) {
- fee=(hours-hourTable[2])*feeTable[2]+2*feeTable[1]+2*feeTable[0];
- }
- if (hours >= 7) {
- fee=(hours-hourTable[3])*feeTable[3]+2*feeTable[2]+2*feeTable[1]+2*feeTable[0];
- }
- System.out.println("停車時數:" + hours + "小時");
- System.out.println("應繳費用:" + fee + "元整");
- }
- }
複製代碼 |