计算利息的计划

时间:2016-02-21 08:27:21

标签: java

这个程序计算给定金额的前6个月的利息,它在错误的地方不起作用?

import java.util.Scanner;

public class BalAfter6Months{

  public static void main(String []args){

    int counter=1;
    double interest,SavBal,total,amount;
    Scanner sc = new Scanner (System.in);
    System.out.print("Enter the monthly saving amount: ");
    amount = sc.nextDouble();

    SavBal = amount;

    while (counter<7) {
      interest = amount * 0.00417;

      total = interest + SavBal;

      amount = amount + SavBal;

      counter++;
   }

      System.out.print(total);

  }

}

2 个答案:

答案 0 :(得分:0)

您已正确编码。您只需初始化total变量以避免编译器错误。

/* Initialize Total Variable */
double interest,SavBal,total = 0,amount;

输入:

100

输出:

102.502

答案 1 :(得分:0)

这避免了不必要的变量,IMO更简单。

import java.util.Scanner;

public class BalAfter6Months{

    public static void main(String []args){

        double interest, balance;

        Scanner sc = new Scanner (System.in);
        System.out.print("Enter the monthly saving amount: ");

        balance = sc.nextDouble();

        int count = 1
        while (counter < 7) {
            interest = balance * 0.00417;
            balance += interest
            counter++;
        }

        System.out.print(total);

    }

}