-4

我已经查看了几次代码,但我不确定是什么影响了它并强制它只使用默认构造函数。例如,如果我尝试输入 2000 作为投资金额,它仍将默认为 1000。

public class Investment {
    private double moneyInvested;
    private double yearsInvested;

    public final static double AMOUNT_DEFAULT = 1000;
    public final static double YEARS_DEFAULT = 5;

    public final static double RATE = 0.12;

    public Investment() {
        moneyInvested = AMOUNT_DEFAULT;
        yearsInvested = YEARS_DEFAULT;
    }

    public Investment(double AMOUNT_DEFAULT, double YEARS_DEFAULT) {
        if (moneyInvested <= 0) {
            moneyInvested = AMOUNT_DEFAULT;
        }

        if (yearsInvested <= 0) {
            yearsInvested = YEARS_DEFAULT;
        }
    }

    public double getMoneyInvested() {
        return moneyInvested;
    }

    public double getYearsInvested() {
        return yearsInvested;
    }

    public void setMoneyInvested(double inputMoney) {
        moneyInvested = inputMoney;

        if (inputMoney <= 0) {
            inputMoney = 1000;
        }
    }

    public void setYearsInvested(double inputYears) {
        yearsInvested = inputYears;

        if (inputYears <= 0) {
            inputYears = 1;
        }
    }

    public static String returnValue(double inputYears, double inputMoney) {
        double returnInvestment;
        int initYears = 1;
        String returnValue = "";

        while (initYears <= inputYears) {
            returnInvestment = Math.pow(1.12, initYears) * inputMoney;
            int investReturn = (int) returnInvestment;

            returnValue = "The amount at end of year " + initYears + " is "
                    + investReturn;

            JOptionPane.showMessageDialog(null, returnValue);
            initYears++;
        }
        return returnValue;
    }
}

public class MakeInvestment {
    public static void main(String[] args) {
        Investment otherClass = new Investment();
        double yA = otherClass.YEARS_DEFAULT;
        double mA = otherClass.AMOUNT_DEFAULT;

        while (inputAmount() == false) {
            inputAmount();
        }

        while (inputYears() == false) {
            inputYears();
        }

        otherClass.returnValue(yA, mA);

    }

    public static boolean inputAmount() {
        String amountAmount = "";

        amountAmount = JOptionPane.showInputDialog(null,
                "Enter the amount to invest (9999).", "Investment Amount",
                JOptionPane.QUESTION_MESSAGE);

        if (amountAmount == null || amountAmount.length() == 0) {
            JOptionPane
                    .showMessageDialog(
                            null,
                            "Nothing entered - You must enter a number for amount invested.",
                            "Investment Amount Error",
                            JOptionPane.ERROR_MESSAGE);
            return false;
        }

        for (int i = 0; i < amountAmount.length(); i++) {
            if (!Character.isDigit(amountAmount.charAt(i))) {
                JOptionPane.showMessageDialog(null,
                        "You must enter a number for amount invested.",
                        "Investment Amount Error", JOptionPane.ERROR_MESSAGE);
                return false;
            }
        }

        double dblAmount = Double.parseDouble(amountAmount);

        return true;
    }

    public static boolean inputYears() {
        String yearAmount = "";

        yearAmount = JOptionPane.showInputDialog(null,
                "Enter the number of years to invest.", "Investment Years",
                JOptionPane.QUESTION_MESSAGE);

        if (yearAmount == null || yearAmount.length() == 0) {
            JOptionPane
                    .showMessageDialog(
                            null,
                            "Nothing entered - You must enter a number for years to invest.",
                            "Investment Years Error", JOptionPane.ERROR_MESSAGE);
            return false;
        }

        for (int i = 0; i < yearAmount.length(); i++) {
            if (!Character.isDigit(yearAmount.charAt(i))) {
                JOptionPane.showMessageDialog(null,
                        "You must enter a number of years to invest.",
                        "Investment Years Error", JOptionPane.ERROR_MESSAGE);
                return false;
            }
        }

        double dblYear = Double.parseDouble(yearAmount);

        return true;
    }
}
4

2 回答 2

4

没有什么是“强迫它使用默认构造函数”。您只是调用了默认构造函数

Investment otherClass = new Investment()

要使用 2 参数构造函数,请传入参数

new Investment(2000.0D, 5.0D)
于 2013-02-18T02:10:23.073 回答
0

我猜这些线:

public final static double AMOUNT_DEFAULT = 1000;
public Investment(double AMOUNT_DEFAULT, double YEARS_DEFAULT)

是问题所在。您声明 AMOUNT_DEFAULT=1000 并将其声明为最终结果。因此,它将始终等于 1000。尝试在构造函数中使用不同的变量名。

编辑:出于兴趣,我很快对此进行了测试,看来代码实际上会按您的预期工作。但是,避免这种变量命名仍然对您有利。

于 2013-02-18T02:11:48.420 回答