Java帮助利率

时间:2013-08-14 13:14:18

标签: java rate

嘿伙计们我正在编写这个程序,除了产生这个错误的利率之外我还能正常工作,

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");
    }

任何人都能告诉我我做错了什么以及如何解决它?干杯

1 个答案:

答案 0 :(得分:1)

将存款声明为双倍。

double deposit = 0D;

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

deposit = (int) balanceCurrent + interest;

但存款是一笔金额,你不应该丢失分数值(小数部分)。

相关问题