我試圖讓三個數組同時運行一個循環。數組的長度不同,並且在最短的數組完成後退出循環。任何幫助是極大的讚賞:Java陣列循環在最短陣列後退出
public class mortgagecalculator4 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
double Loan1[] = {200000, 360, ((5.75/12)/100)};//declares array
//and defines variables for first loan
double Loan2[] = {200000, 180, ((5.50/12)/100)};//declares array
//and defines variables for second loan
double Loan3[] = {200000, 84, ((5.35/12)/100)};//declares array
//and defines variables for third loan
double Payment1;//establishes variable for first payment
double Payment2;//establishes variable for second payment
double Payment3;//establishes variable for third payment
double Interest1;//establishes variable for interest for loan 1
double Interest2;//establishes variable for interest for loan 2
double Interest3;//establishes variable for interest for loan 3
double Balance1 = Loan1[0];//declares balance for loan 1
double Balance2 = Loan2[0];//declares balance for loan 2
double Balance3 = Loan3[0];//declares balance for loan 3
double Principal1;// declares variable for principal 1
double Principal2;// declares variable for principal 2
double Principal3;// declares variable for principal 3
Payment1 = (Loan1[0] * (Loan1[2]))/(1 - Math.pow(1/(1 + Loan1[2]),
Loan1[1]));//calculates monthly payment for first loan
Payment2 = (Loan2[0] * (Loan2[2]))/(1 - Math.pow(1/(1 + Loan2[2]),
Loan2[1]));//calculates monthly payment for second loan
Payment3 = (Loan3[0] * (Loan3[2]))/(1 - Math.pow(1/(1 + Loan3[2]),
Loan3[1]));//calculates monthly payment for third lona
System.out.printf("The monthly payments are: Loan One:$%.2f ",
Payment1);//prints monthly payment amount for first loan
System.out.printf("Loan Two:$%.2f ",
Payment2);//prints monthly payment amount for second loan
System.out.printf("Loan Three:$%.2f\n",
Payment3);//prints monthly payment amount for third loan
System.out.printf("Each monthly payment breaks down as follows:\n");
//displays 'each monthly payment...'
for (int PaymentNumber = 1; PaymentNumber <= Loan1[1]
&& PaymentNumber <= Loan2[1] && PaymentNumber <= Loan3[1];
PaymentNumber++) {
//creates 'for' loop that continues until the LoanTerm variable
//is 0
Interest1 = Balance1 * Loan1[2];//Determines amt paid toward interest
Principal1 = Payment1 - Interest1;//Determines amt paid toward principal
Balance1 = Balance1 - Principal1;//Creates new balance for loop
PrintStream printf = System.out.printf(" Principal is: $%.2f", Principal1);
//prints out the amount of principal paid
System.out.printf(" Interest is: $%.2f", Interest1);
//prints otu the amount of interest paid
System.out.printf(" New balance is: $%.2f", Balance1);
//prints out the new balance amount
Interest2 = Balance2 * Loan2[2];//Determines amt paid toward interest
Principal2 = Payment2 - Interest2;//Determines amt paid toward principal
Balance2 = Balance2 - Principal2;//Creates new balance for loop
System.out.printf(" Principal is: $%.2f", Principal2);
//prints out the amount of principal paid
System.out.printf(" Interest is: $%.2f", Interest2);
//prints otu the amount of interest paid
System.out.printf(" New balance is: $%.2f", Balance2);
//prints out the new balance amount
Interest3 = Balance3 * Loan3[2];//Determines amt paid toward interest
Principal3 = Payment3 - Interest3;//Determines amt paid toward principal
Balance3 = Balance3 - Principal3;//Creates new balance for loop
System.out.printf(" Principal paid is: $%.2f", Principal3);
//prints out the amount of principal paid
System.out.printf(" Interest paid is: $%.2f", Interest3);
//prints otu the amount of interest paid
System.out.printf(" New balance is: $%.2f\n", Balance3);
//prints out the new balance amount
try {
Thread.sleep(500);//tells program to sleep for two seconds
} catch (InterruptedException ex) //catches any exceptions that are thrown
{
}
}
}
}
你所說的「在同一時間」是什麼意思?你希望發生什麼,因爲它們長度不同? –
你還沒有告訴我們什麼是錯的。 (我也強烈建議你的代碼應該被重構,特別是封裝貸款的值,你現在有一個數組。) –
@Dave:我假設「在同一時間」意思是「在同一個循環體中」。 – Vlad