Java传递数组到计算和返回数组的方法

时间:2012-04-01 21:54:42

标签: java arrays methods

我遇到了关于如何将数组传递给方法的文章“pass array to method Java”但是当我尝试将其调整到我的intrest计算器时,我得到了一些错误,例如“运算符/未定义为参数类型double [],double“,我无法解决它。我也希望返回整个数组,因为我需要稍后打印结果并最终对它们进行排序。

例如,我定义了我的阵列以及它们的大小,然后呼叫用户输入计算的贷款金额,利息和频率。在我获取数据及其全部为数组格式之后,我将关闭整个数组以计算简单兴趣并希望将结果作为数组返回,然后传递相同的初始数组以按月,周和日查找复合兴趣但是当我尝试进行“A = P(1+(r / n))^(n * t)”中的划分时,我得到了前面提到的错误。收集数据工作正常我似乎无法正确传递数组或如果我做的话,我会循环通过它们

任何和所有的帮助都会一如既往地受到赞赏。

相关代码

从用户调用数据

do {
        arrPrincipalAmt[i] = getPrincipalAmount();
        arrInterestRate[i]= getInterestRate();
        arrTerm[i] = getTerm();
        i++;
        if (i < 4)
        {
          /*
           * if "i" comes to equal 4 it is the last number that will fit in the array of 5
           * and will Skip asking the user if they want to input more
           */

          System.out.println("Enter another set of Data? (Yes/No):");
          boolean YN = askYesNo();
          if (YN == true)
          {
            more = true;
          }
          else if (YN == false)
          {
            more=false;
          }
        }
        else
        {
          more = false;
        }
      }while(more);


      while (run)
      {

        //Start calculations call methods
        final int dINy = 365; //days in year
        final int mINy = 12; //months in year
        final int wINy = 52; //weeks in year
        double loanYears =(double) arrTerm[i]/mINy; //convert loan months into fraction of years, or whole years with decimals.
        arrSimple= calculateSimpleInterest(arrPrincipalAmt, arrInterestRate,loanYears);
        //Simple IntrestloanAmount * (annualInterestRate/100.0) * loanYears;
        arrCompoundMonthly = calculateCompundInterest(arrPrincipalAmt, arrInterestRate,mINy,loanYears);
        //rewrite month compound:loanAmount * Math.pow((1+((annualInterestRate/100)/mINy)),(loanYears*mINy)) - loanAmount;

失败的简单兴趣

public static double[] calculateSimpleInterest(double[] arrPrincipalAmt, double[] arrInterestRate, double Years)
  {
    for(int i=0; i<arrPrincipalAmt.length; i++)
    {
      double simpInterest= arrPrincipalAmt[i] * (arrInterestRate[i]/100.0) * Years; //Simple Interest Calculation
    }
    return simpInterest;
  }

失败的复利

 public static double[] calculateCompundInterest(double[] arrPrincipalAmt, double[] 

arrInterestRate, double frequency, double time)
  {
    /*The Compound Interest calculation formation is as follows:A = P(1+ (r/n))^(n*t) - P
     A = total interest amount only.
     P = principal amount (loan amount or initial investment).
     r = annual nominal interest rate (as a decimal not a percentage).
     n = number of times the interest is compounded per year.
     t = number of years (don't forget to convert number of months entered by user to years).
     */
    for (int i=0; i < arrPrincipalAmt.length; i++)
    {
      double[] compound= arrPrincipalAmt[i] * Math.pow((1+(arrInterestRate[i]/100.0)/frequency),(frequency*time)) - arrPrincipalAmt[i]; //Compound Interest  monthly, weekly and daily depending on the frequency passed
    }
    return compound;
  }

2 个答案:

答案 0 :(得分:3)

如果您收到类似the operator / is undefined for the argument types double[],double的错误,则表示您尝试将整个数组除以值而不是单个元素。

myarray / value之类的行更改为myarray[i] / value

答案 1 :(得分:1)

我检查了你的代码,问题是你在第227行将化合物定义为一个双数组,而该行的表达式结果只是一个双精度数。 我想你想这样做:

double[] compound = new double[arrPrincipalAmt.length];

for (int i=0; i < arrPrincipalAmt.length; i++) {
    compound[i] = arrPrincipalAmt[i] * Math.pow((1+(arrInterestRate[i]/100.0)/frequency),(frequency*time)) - arrPrincipalAmt[i];
}

return compound;