我正在尝试执行我的java代码,但它显示时间太长。有一个弹出的方法,说要检查我是否有一个无限循环。我不知道。至少,我不认为我知道。我是java新手,所以我真的需要帮助。我的赋值需要一个for循环,这是我想出来的代码
import java.lang.Math;
public class Main{
double initialBalance;
double interestRate;
double years;
double totalPayment;
double firstInterest;
double lastInterest;
double totalInterest;
double totalPrincipal;
double firstPrincipal;
double lastPrincipal;
double firstBalance;
double lastBalance;
double monthlyPayment;
public static final double periodsPerYear = 12;
public static final double invalidNum = -1.0;
public Main(double balance, double interest, double Life){
initialBalance = balance;
interestRate = interest;
years = Life;
//monthlyPayment calculation
monthlyPayment = initialBalance * ((interestRate/periodsPerYear)+((interestRate/periodsPerYear)/(Math.pow(1+(interestRate/periodsPerYear),periodsPerYear*years)-1)));
for (double i=0;i<periodsPerYear*years;i++){
interest = (interestRate/periodsPerYear) * initialBalance;
totalInterest += interest;
double principal = monthlyPayment - interest;
totalPrincipal += principal;
initialBalance = initialBalance - principal;
//if statement
if(i==0){
firstInterest = interest;
firstPrincipal = principal;
firstBalance = initialBalance;
}
if(i== (12*2)-1){
lastInterest = interest;
lastPrincipal = principal;
lastBalance = initialBalance;
}
}
totalPayment = totalPrincipal + totalInterest;
}
double getMonthlyPayment(){
return monthlyPayment;
}
double getAmount(String month, String type){
if (month.equals("first")&&type.equals("balance")){
valid = firstBalance;
}
else if (month.equals("first")&&type.equals("interest")){
valid = firstInterest;
}
else if (month.equals("first")&&type.equals("principal")){
valid = firstPrincipal;
}
else if (month.equals("last")&&type.equals("balance")){
valid = lastBalance0;
}
else if (month.equals("last")&&type.equals("interest")){
valid = lastInterest;
}
else if (month.equals("last")&&type.equals("principal")){
valid = lastPrincipal;
}
else{
valid = invalidNum;
}
return Math.round((valid *100.00)/100.00);
}
double getTotalInterest(){
return totalInterest;
}
double getTotalPayments(){
return totalPayment;
}
public static void main(String[] args){
double initialBalance = 10_000.0;
double interestRate = 0.05;
double years = 2;
Main loan = new Main( initialBalance, interestRate, years );
System.out.println( loan.getMonthlyPayment() );
System.out.println( loan.getTotalInterest() );
System.out.println( loan.getTotalPayments() );
System.out.println( loan.getAmount( "first", "interest" ) );
System.out.println( loan.getAmount( "first", "principal" ) );
System.out.println( loan.getAmount( "first", "balance" ) );
System.out.println( loan.getAmount( "first", "!@#$%^&*()" ) );
System.out.println( loan.getAmount( "last", "interest" ) );
System.out.println( loan.getAmount( "last", "principal" ) );
System.out.println( loan.getAmount( "last", "balance" ) );
System.out.println( loan.getAmount( "last", "{}|:<>?" ) );
System.out.println( loan.getAmount( "{)*(&EPQWR", "interest") );
}
}我该怎么做才能让它执行得更快呢?我做错了什么?
发布于 2020-02-24 16:12:13
首先,您提供的代码不能编译。getAmount()中的变量'valid‘和'lastBalance0’未声明。可能你忘了在这个方法的顶部声明'double valid‘,而只键入了'lastBalance0’而不是'lastBalance‘。
您的代码还存在一些其他问题:
发布于 2020-02-24 16:35:52
正如Alex所指出的,您的代码不能编译。
在修复了这两件事之后,它确实运行了:
代码运行后,输出如下:
Mon Feb 24 09:34:37 CET 2020
1582533277204
4387.13897340686
5291.335361764231
105291.33536176465
417.0
3970.0
96030.0
-1.0
18.0
4369.0
0.0
-1.0
-1.0
Mon Feb 24 09:34:37 CET 2020
1582533277206我在开始和结束时在millis中添加了当前日期和时间的打印,程序花了2毫秒执行。
https://stackoverflow.com/questions/60371212
复制相似问题