2011-10-20 51 views
-4

我試圖讓三個數組同時運行一個循環。數組的長度不同,並且在最短的數組完成後退出循環。任何幫助是極大的讚賞: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 
      { 
      } 
     } 
    } 
} 
+2

你所說的「在同一時間」是什麼意思?你希望發生什麼,因爲它們長度不同? –

+0

你還沒有告訴我們什麼是錯的。 (我也強烈建議你的代碼應該被重構,特別是封裝貸款的值,你現在有一個數組。) –

+0

@Dave:我假設「在同一時間」意思是「在同一個循環體中」。 – Vlad

回答

1

爲什麼不

int minLength = Math.min(Math.min(arr1.size(), arr2.size()), arr3.size()); 
for (int i = 0; i < minLength; i++) 
{ 
    ... 

+0

我猜迭代次數不是實際上數組長度,而是第二個數組元素的值(即360,180和84),並且循環在PaymentNumber達到84時停止。 - 然而,這很奇怪,他正在迭代3個(看起來不相關的)數組。 – Thomas

+0

@Thomas:這是這樣寫的,但我認爲這是代碼中的錯誤。 – Vlad

+0

那麼,所有3個陣列的長度都是3,所以沒有一個更短。因此我假設「最短陣列」在這裏是錯誤的術語。 – Thomas

0

好,因爲這顯然是功課,我會在這裏提供指導。你遇到困難的情況是你有3個數組,並且需要在循環的相同迭代中遍歷每個數組。如果這不正確,請發表評論,我會更正這些陳述。所以這裏的一般問題是你有一個需要處理集合中的X元素的循環。這裏需要記住的是,我們知道必須發生的迭代次數,這相當於最大采集長度。所以,如果有您的收藏中的10個元素的循環需要去,直到10

//一些僞
的for(int i = 0;我< 10;我++)// 10 ==規模最大的集大小
{...}

現在問題在於您有x-1個較小的集合,這會阻止您訪問大於x sub i的大小屬性的元素。所以要解決這個問題,我們需要跟蹤最後處理的元素索引。如此說來,第一件事是創建具有以下屬性的循環:

對(INT I = 0;我< collection.minSize;我++){...}

記錄的最後一個索引被處理,這將允許你餵養後續循環來處理剩餘的集合。

0

您當前的循環將停止一旦PaymentNumber是84,因爲這是滿足循環條件(這可以理解爲PaymentNumber <= 360 and PaymentNumber <= 180 and PaymentNumber <= 84,注意PaymentNumber必須比所有這些更低)的最高值。

看起來這三個數組並沒有相互關聯,所以你可能對每個數組使用3個不同的循環或更好的方法,編寫一個以貸款(數組)爲參數幷包含循環的方法:

private static double handleLoan(double payment, double initialBalance, double[] loan) { 
    double balance = initialBalance; 

    for (int paymentNumber = 1; paymentNumber <= loan[1]; paymentNumber ++) { 
    interest = balance * loan[2]; 
    double principal = payment - interest; 
    balance = balance - principal; 

    System.out.printf(" Principal is: $%.2f", principal); 

    System.out.printf(" Interest is: $%.2f", interest); 
    System.out.printf(" New balance is: $%.2f", balance); 
    } 
    return balance; 
} 

然後調用它像這樣(請注意我用的變量,看到底部的說明):

Balance1 = handleLoan(Payment1, Balance1, Loan1); 

一對夫婦的旁註:

  1. 以大寫字母開頭的變量/參數名稱是不好的做法,也違背了命名約定。
  2. 重構你的貸款數組對象,因爲數組不是類型安全,並可能有不同的長度(見喬恩長柄水杓評論)