Java,计算以下公式
Java, computing the following formula
我正在尝试使用 Java 在图像中写入公式。但是,我得到了错误的输出。这就是我到目前为止的计算。
假设输入为:Loan Amount = 50000 and Length = 3 year。
我目前的输出是 'Monthly Payment' 的“676.08”和“总利息支付”的“25661.0”。
“每月付款”应为“1764.03”,“总利息付款”应为“13505.05”。
代码是:
//Program that displays the interest rate and monthly payment based on the loan amount and length of loan the user inputs.
import java.util.Scanner;
public class bankLoanSelection{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
//Declarations.
double interestRate;
final double n = 12;
//Generates an account number in the range of 1 to 1000.
int accountnumber = (int)(Math.random() * 1000)+1;
//User input
System.out.print("Enter Loan Amount: $");
double L = input.nextDouble();
System.out.print("Length of Loan (years): ");
double i = input.nextDouble();
//selection structure using if and if-else statements.
if (L <= 1000)
interestRate = .0825;
else if (L <= 5000)
interestRate = .0935;
else if (L <= 10000)
interestRate = .1045;
else interestRate = .1625;
//Computations.
double MonthlyPayment = L * (interestRate / n) * Math.pow(1 + interestRate / n, i * n) / Math.pow(1 + interestRate / n, i * n) - 1;
double TotalAmountOfLoan = MonthlyPayment * i * n;
double TotalInterest = L - TotalAmountOfLoan;
//Output.
System.out.println("Account Number: #" + accountnumber);
System.out.println("Loan Amount: $" + L);
System.out.println("Loan Length (years): " + i);
System.out.println("Interest Rate: " + interestRate * 100 + "%");
System.out.printf("Total Monthly Payment: $%.2f%n", MonthlyPayment); //Rounds second decimal
System.out.println("Total Interest Payments: $" + TotalInterest);
}
}
您需要更正分母中括号的排列,正确的公式为:
double numerator = L * (interestRate / n) * Math.pow(1 + interestRate / n, i * n)
double denominator = Math.pow(1 + interestRate / n, i * n) - 1;
double MonthlyPayment = numerator / denominator;
尝试将问题分解成更小的单元来解决。
你必须在条件之间加上圆括号。
应该是:
double MonthlyPayment = L * ((interestRate / n) * Math.pow(1 + interestRate / n, i * n))
/ (Math.pow(1 + interestRate / n, i * n) - 1);
我正在尝试使用 Java 在图像中写入公式。但是,我得到了错误的输出。这就是我到目前为止的计算。
假设输入为:Loan Amount = 50000 and Length = 3 year。
我目前的输出是 'Monthly Payment' 的“676.08”和“总利息支付”的“25661.0”。
“每月付款”应为“1764.03”,“总利息付款”应为“13505.05”。
代码是:
//Program that displays the interest rate and monthly payment based on the loan amount and length of loan the user inputs.
import java.util.Scanner;
public class bankLoanSelection{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
//Declarations.
double interestRate;
final double n = 12;
//Generates an account number in the range of 1 to 1000.
int accountnumber = (int)(Math.random() * 1000)+1;
//User input
System.out.print("Enter Loan Amount: $");
double L = input.nextDouble();
System.out.print("Length of Loan (years): ");
double i = input.nextDouble();
//selection structure using if and if-else statements.
if (L <= 1000)
interestRate = .0825;
else if (L <= 5000)
interestRate = .0935;
else if (L <= 10000)
interestRate = .1045;
else interestRate = .1625;
//Computations.
double MonthlyPayment = L * (interestRate / n) * Math.pow(1 + interestRate / n, i * n) / Math.pow(1 + interestRate / n, i * n) - 1;
double TotalAmountOfLoan = MonthlyPayment * i * n;
double TotalInterest = L - TotalAmountOfLoan;
//Output.
System.out.println("Account Number: #" + accountnumber);
System.out.println("Loan Amount: $" + L);
System.out.println("Loan Length (years): " + i);
System.out.println("Interest Rate: " + interestRate * 100 + "%");
System.out.printf("Total Monthly Payment: $%.2f%n", MonthlyPayment); //Rounds second decimal
System.out.println("Total Interest Payments: $" + TotalInterest);
}
}
您需要更正分母中括号的排列,正确的公式为:
double numerator = L * (interestRate / n) * Math.pow(1 + interestRate / n, i * n)
double denominator = Math.pow(1 + interestRate / n, i * n) - 1;
double MonthlyPayment = numerator / denominator;
尝试将问题分解成更小的单元来解决。
你必须在条件之间加上圆括号。
应该是:
double MonthlyPayment = L * ((interestRate / n) * Math.pow(1 + interestRate / n, i * n))
/ (Math.pow(1 + interestRate / n, i * n) - 1);