Java阵列循环在最短阵列之后退出

时间:2011-10-20 14:26:32

标签: 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
            {
            }
        }
    }
}

3 个答案:

答案 0 :(得分:1)

为什么不

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

答案 1 :(得分:0)

好的,因为这显然是家庭作业,我将在这里提供指导。您遇到困难的情况是您有3个数组,需要在循环的同一次迭代中循环遍历每个数组。如果这不正确,请发表评论,我将更正这些陈述。所以这里的一般问题是你有一个循环需要处理集合中的X元素。这里要记住的是我们知道必须发生的迭代次数,这相当于最大收集长度。因此,如果集合中有10个元素,则循环需要持续到10个。

//一些伪代码
for(int i = 0; i&lt; 10; i ++)// 10 ==最大集合尺寸
{...}

现在问题是你有x-1较小尺寸的集合,这会阻止你访问大于x sub i大小属性的元素。因此,为了解决这个问题,我们需要跟踪最后处理的元素索引。话虽这么说,首先要创建一个具有以下属性的循环:

for(int i = 0; i&lt; collection.minSize; i ++){...}

记录最后处理的索引,这将允许您提供后续循环以处理剩余的集合。

答案 2 :(得分:0)

PaymentNumber为84时,您的当前循环将停止,因为这是满足循环条件的最高值(可以读作PaymentNumber <= 360 and PaymentNumber <= 180 and PaymentNumber <= 84,请注意PaymentNumber必须低于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; } 所有这些)。

似乎三个数组彼此无关,所以你可能只为每个数组使用3个不同的循环或更好,编写一个方法,将贷款(数组)作为参数并包含循环:

Balance1  = handleLoan(Payment1, Balance1, Loan1 );

然后像这样调用它(注意我使用你的变量,见底部的旁注):

{{1}}

几个旁注:

  1. 以大写字母开头的变量/参数名称是不好的做法,也不符合命名约定。
  2. 将您的贷款数组重构为对象,因为数组不是类型安全的,可能有不同的长度(请参阅Jon Skeets评论)