ループごとに新しい値を取得しようとする問題を解決しようとしています。プログラムは、利息の支払い、元金の支払い、現在の残高に罰金を科すと想定されているため、ループするたびに利息が少なくなるはずですが、最初の回答を繰り返しループするだけです
これは最初の答えが正しい出力ですが、他の3つは
Enter Loan Amount:500
Enter Annual Interest:50
Total payment:4
Enter Loan Length :1
Interest PaymentPrincipal PaymentCurrent Balance
Interest Payment Principal Payment Current Balance 62.5
62.5
62.5
62.5
最初の支払い後、残高の 12.5% になるはずです。
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//variabled decleared
double rate;
double payment;
int amt = 1;
//input
System.out.print("Enter Loan Amount:");
double principal = input.nextDouble();
System.out.print("Enter Annual Interest:");
double interest = input.nextDouble();
System.out.print("Total payment:");//12=monthly,4= quartely,2=semi-annually and 1=annually
double period = input.nextDouble();
System.out.print("Enter Loan Length :");
int length = input.nextInt();
//proces
rate = interest / 100;
double period_rate = rate / period;
double n = period * length;
payment = (principal*Math.pow((1+period_rate),n))/n;
System.out.printf("\n"+"Interest Payment"+" Principal Payment "+" Current Balance ");
for(int i=1; i<=n; i++){
double principal_payment=0;
double current_balance;
double payment_interest;
current_balance=(principal-principal_payment);
payment_interest=(principal*period_rate);
principal_payment=(payment-payment_interest);
principal=current_balance;
System.out.println(payment_interest+"");
}