📄 loanrepay.java
字号:
import java.util.Scanner;
public class LoanRepay
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner (System.in);
float A,R,r,b,B;
int Y,M,i;
// A stands for the loan amount
// R stands for the monthly repayment
// r stands for the net annual interest rate(without %)
// Y stands for the number of whole remaining years
// M stands for the number of remaining months
System.out.print("Please input the loan amount: $");
A = keyboard.nextFloat();
System.out.print("Please input the net annual interest rate(r%): r=");
r = keyboard.nextInt();
System.out.print("Please input the number of whole remaining years: Y=");
Y = keyboard.nextInt();
System.out.print("Please input the number of remaining months: M=");
M = keyboard.nextInt();
// display the input requirements and the entered datas
r = r/100;
b = 1+r;
B = 1;
if (A>=0&&r>=0&&Y>=0&&M>=0)
{
for (i=1 ; i<=Y ; i++)
{
B = B*b;
}
// calculate the (1+r)^Y
R = (A*r*B*(12+r*M))/(12*(r*M+((12+r*M)*(B-1))));
// use this formula to calculate the monthly repayment
System.out.print("The monthly repayment R is $");
System.out.printf("%.2f",R);
}
else
{
if (A<0)
System.out.println("Some error message for the invalid amount!");
if (r<0)
System.out.println("Some error message for the invalid interest rate!");
if (Y<0)
System.out.println("Some error message for the invalid year(s)!");
if (M<0)
System.out.println("Some error message for the invalid months!");
// when something wrong with the inputs, the relative information will be displayed on the screen
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -