0

嘿伙计们,我正在编写这个程序,除了利率导致这个错误之外,我一切正常,

MenuDrivenProgram.java:92: possible loss of precision
found : double
required: int
deposit = balanceCurrent + interest;
^
1 error

这是我的代码,

public static void InvestmentReport()
    {
        System.out.printf("*** Investment Report stub ***\n");
        // This is where your Part A solution goes
        System.out.printf("*************** InvestmentReport Menu ***************\n\n");
        Scanner console=new Scanner(System.in);
        int deposit, monthly, interestRate;
        double interest, balanceCurrent;
        System.out.println ("Enter your initial deposit amount in dollars\n");
        deposit = console.nextInt();

        System.out.println ("Enter the annual interest rate as a percentage (eg. 6.0)\n");
        interest = console.nextDouble();

        System.out.println ("Enter your monthly deposit amount in dollars\n");
        monthly = console.nextInt();

        System.out.println ("Savings growth over the next 6 months:\n");

        System.out.println ("Balance after first month: $" + deposit);
        deposit = deposit + monthly;
        System.out.println ("Interest earned for this month: $" + interest);
        interest = balanceCurrent * interestRate / 12 / 100;
        deposit = balanceCurrent + interest;

        System.out.println ("Balance after second month: $" + deposit);
        deposit = deposit + monthly;
        System.out.println ("Interest earned for this month:\n");

        System.out.println ("Balance after third month: $" + deposit);
        deposit = deposit + monthly;
        System.out.println ("Interest earned for this month:\n");

        System.out.println ("Balance after fourth month: $" + deposit);
        deposit = deposit + monthly;
        System.out.println ("Interest earned for this month:\n");

        System.out.println ("Balance after fifth month: $" + deposit);
        deposit = deposit + monthly;
        System.out.println ("Interest earned for this month:\n");

        System.out.println ("Balance after sixth month: $" + deposit);
        deposit = deposit + monthly;
        System.out.println ("Interest earned for this month:\n");
    }

谁能告诉我我做错了什么以及如何解决?干杯

4

1 回答 1

1

声明存款为双倍。

double deposit = 0D;

或者,您可以将值转换为 int。

deposit = (int) balanceCurrent + interest;

但是存款是一个金额,我想你不应该失去美分价值(小数部分)。

于 2013-08-14T13:17:16.040 回答