扫描Java中的百分比的问题

问题描述 投票:0回答:1

我正在尝试自学Java语言,实际上我有一个练习任务的问题,我需要在代码中添加感兴趣的百分比,我可以在循环结束时计算数量。我如何告诉程序将扫描的兴趣值作为“百分比”并使用它循环,因为我正在尝试? 。

public static void main(String[] args) {
    int amount;
    int year=0;
    double interest=0;

    Scanner sc = new Scanner(System.in);
    System.out.println("What is the amount?");
    amount = sc.nextInt();
    System.out.println("How much is the interest");
    interest= sc.nextDouble();
    while(amount <= 2000)
    {
    year++;
    interest =amount*(interest/100);
    amount =(int) (amount+interest);

    }

    System.out.println("amount"+amount);
    System.out.println("year"+year);
java percentage
1个回答
0
投票

您可以像这样简单地更改循环,并避免更改兴趣变量值:

while(amount <= 2000)
{
    year++;
    amount = (int) (amount + (amount*(interest/100)));
}
© www.soinside.com 2019 - 2024. All rights reserved.