Java计算停车费用的应用

计算停车费用。收费标准为:停车收费起始价¥6,停车3小时;超过3小时,按照每小时追加¥3,一天内一次停车收费最多不超过¥36。假定一次停车不超过24小时。编写程序,通过键盘录入停车小时数(整数),然后计算停车费并输出。

 public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int hours = input.nextInt();
        int fee = 0;
        if (hours <= 3) {
            fee = 6;
        } else if (hours <= 24) {
            fee = 6 + (hours - 3) * 3;
            if (fee > 36) {
                fee = 36;
            }
        }
        System.out.println("停车" + hours + "小时,停车费用为" + fee + "元。");
    }

import java.util.Scanner;

public class CSDNQ7912027 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入停车小时数:");
        int hours = scanner.nextInt();
        scanner.close();
        double fee;
        if (hours <= 3) {
            fee = 6.0;
        } else if (hours <= 24) {
            fee = 6.0 + Math.min(30.0, (hours - 3) * 3.0);
        } else {
            fee = 36.0;
        }
        System.out.printf("停车费用为:¥%.2f%n", fee);
    }
}