📄 loanrepay.java
字号:
import java.util.Scanner;
public class LoanRepay {
public static void main(String[] args) {
double repayment,loanAmount;
int interest,wholeYear,month;
String reUse;//store the user command of whether reuse the program.
boolean correctInput = true;//control the loop of reusing the program.
Scanner keyboard = new Scanner(System.in);
System.out.println("Please input the amount of loan");
System.out.println("without dollar sign.");
loanAmount = keyboard.nextDouble();
System.out.println("Please input the annual interst rate");
System.out.println("expressed as an integer. (e.g. 10 which means 10%)");
interest = keyboard.nextInt();
System.out.println("Please input the number of whole Years");
System.out.println("follow by the number of Months.");
wholeYear = keyboard.nextInt();
month = keyboard.nextInt();
if (loanAmount < 0)
{
System.out.println("Some error message for the invalid amount!");
correctInput = false;
};//identify the illegal input and display the error message.
if (interest < 0)
{
System.out.println("Some error message for the invalid interest rate!");
correctInput = false;
};//identify the illegal input and display the error message.
if (wholeYear < 0)
{
System.out.println("Some error message for the invalid year(s)!");
correctInput = false;
};//identify the illegal input and display the error message.
if (month <0)
{
System.out.println("Some error message for the invalid month(s)!");
correctInput = false;
};//identify the illegal input and display the error message.
if (correctInput)
{
System.out.printf("The loan is $ %.2f",loanAmount);
System.out.printf(" at %d%% interest. %n",interest);
System.out.printf("over %d year(s) %d month(s).%n",wholeYear,month);
double percentage = (double) interest/100;//computing the real interest for calculating
double temporary1,temporary2,temporary3;//temporary variables to make the calculation more simple.
temporary1 = Math.pow(1+percentage, wholeYear);
temporary2 = loanAmount*percentage*temporary1*(12+percentage*month);
temporary3 = (temporary1-1)*(12+percentage*month)+percentage*month;
repayment = temporary2/(12*temporary3);//computting the repayment.
System.out.printf("The monthly repayment is $ %.2f%n",repayment);
System.out.println("Do you want to reuse the service?");
System.out.println("input Yes to restart, or other keys to exit.");
reUse = keyboard.next();//load the user command.
if (reUse.equalsIgnoreCase("Yes"))
{
System.out.println();
LoanRepay.main(args);//restart the program.
}
else
System.out.println("Thanks for using LoanRapaymentCalculation!");
}
else
{
System.out.printf("%nPlease reinput the appropriate numbers!%n");
LoanRepay.main(args);
};//automatically restart the program because of the inappropriate input.
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -