我目前正在开发一个程序,该程序将计算贷款的简单利率和每月付款。虽然,我遇到了一个很大的问题。我正在努力使我的主要价值(见代码)重新分配给我的新余额的价值(见代码)。这是我现在的代码,我将在下面更详细地解释:
import java.util.Scanner;
public class Payments {
public static double principal; //principal
public static double annualrate; //annual interest rate
public static double p; //monthly payment
public static double mr; //monthly interest rate
public static double nb; //new balance after monthly payments
public static double i; //interest (monthly)
public static String spaces = " "; //spaces for making clean columns
public static int months = 12;
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Principal: $");
principal = input.nextDouble();
System.out.println("Anual Interest Rate: ");
annualrate = input.nextDouble();
System.out.println("Monthly Payment: $");
p = input.nextDouble();
calculate();
}
public static void calculate() {
mr = annualrate / 12;
i = mr * p;
nb = principal + i - p;
System.out.print("Month Principal Amt. Interest Payment New Balance");
System.out.println();
System.out.println();
for(int x = 1; nb > 0; nb = principal + i - p){
System.out.println(x + spaces + p + spaces + i + "%" + spaces + "$" + p + spaces + "$" + nb);
p = (Double)null;
p = nb;
}
}
}
因此,正如您很可能通过代码中的注释看到的那样,显示了所有变量。现在,忽略 null 而我将其转换为双精度值,因为这是我在问你们之前尝试做的最后一件事 :) 无论如何,我的最后一个问题是,我怎样才能将 principal 的值重新分配给我的新百伦(NB)?另外,一个附带的问题,对于这种程序来说,while循环会更好吗?