01/10/2018, 01:18

[Giải bài tập]Viết chương trình tính tiền điện bằng phương pháp lũy tiến

Viết chương trình nhập vào số điện sử dụng của tháng và tính tiền điện theo phương pháp lũy tiến
Nếu số điện 0 đến 50 thì đơn giá là 1000 tien=soDien1000
nếu >50 thì là 1200 tien=50
1000+(soDien -50)*1200
mình đang bí bài này. Mong mọi người giúp đỡ

Nguyễn Văn Tâm viết 03:34 ngày 01/10/2018
if (soDien < 50) {
    // Tính số tiền khi số điện nhỏ hơn 50
} else if (soDien < 150) {
    // Tính số tiền khi số điện từ 50-149
} else if (...) {
    //Tiếp tục
}
Nguyễn Hải Dương viết 03:28 ngày 01/10/2018

Quân viết 03:24 ngày 01/10/2018
import java.util.Arrays;

public class ElectricComputation {

    public static void main(String[] args) {
        ElectricComputation computation = new ElectricComputation();
        computation.loadPrices(new int[]{50, 100, 150, 200}, new double[]{1, 1.2, 1.5, 1.75, 1.80});
        System.out.println(computation.calculateCost(160));
    }

    private int[] amountSteps;
    private double[] unitPrices;

    public ElectricComputation() {
        loadPrice(new int[]{}, new double[]{1});
    }

    public void loadPrices(int[] electricStep, double[] prices) {
        if (electricStep == null) {
            throw new IllegalArgumentException("electricStep could not be null");
        }
        if (prices == null || prices.length <= electricStep.length) {
            throw new IllegalArgumentException("price table not valid, must be not null and equal price limits count + 1");
        }
        amountSteps = new int[electricStep.length];
        for (int i = 0; i < electricStep.length; i++) {
            if (i == 0) {
                if (electricStep[0] < 0) {
                    throw new IllegalArgumentException("First step must be greater than zero");
                }
                amountSteps[0] = electricStep[0];
            } else {
                amountSteps[i] = electricStep[i] - amountSteps[i - 1];
                if (amountSteps[i] <= 0) {
                    throw new IllegalArgumentException("Electric step " + i + "must be greater than previous step, found: " + electricStep[i]);
                }
            }
        }
        this.unitPrices = Arrays.copyOf(prices, electricStep.length + 1);
    }

    public double calculateCost(final int totalUsed) {
        if (totalUsed < 0) {
            throw new IllegalArgumentException("electric number must be positive number");
        }
        if (amountSteps == null) {
            return totalUsed;
        }
        final int AMOUNT_STEP_COUNT = amountSteps.length;
        if (AMOUNT_STEP_COUNT == 0) {
            return (double) totalUsed * unitPrices[0];
        }
        double totalCost = 0;
        int remainNumber = totalUsed;
        for (int amountStepIndex = 0; amountStepIndex < AMOUNT_STEP_COUNT; amountStepIndex++) {
            int calcNumber = Math.min(remainNumber, amountSteps[amountStepIndex]);
            remainNumber -= amountSteps[amountStepIndex];
            totalCost += (double) calcNumber * unitPrices[amountStepIndex];
            if (remainNumber <= 0) {
                break;
            }
        }
        if (remainNumber > 0) {
            totalCost += (double) remainNumber * unitPrices[AMOUNT_STEP_COUNT];
        }
        return totalCost;
    }
}

Nhập số điện lũy tiến và giá tiền lũy tiến, khi thay đổi yêu cầu sẽ không cần phải viết lại chương trình. OK. Khuyến khích dùng để tham khảo, trong đây có gần như đầy đủ cú pháp và cách vận dụng syntax cơ bản của java.

Bài liên quan
0