1

我的问题是基于复利公式。A = P(1 + r)^n。我有许多 r 和 n 值,我必须将它们存储为数组。我还必须将我的最终值范围 A 也输出为数组。我想我已经将 r 和 n 正确地存储为一个数组。然而,我的问题在于 A 的最终值并存储每个值 A。到目前为止,这就是我所写的。

import java.util.Scanner;
import javax.swing.*;

public class Prin {
public static void main (String [] args){

    System.out.println("Please enter the Principal you wish to invest =>");
    Scanner stdio = new Scanner (System.in);
    int principal = stdio.nextInt(); 
    stdio.nextLine();

    System.out.println("This program will now calculate the final amount A for (years) n = 1,2,3,4 and the rate r = 1%, 1.5%, 2%, 2.5%, 3%");

    int yearsarray[] = {1,2,3,4};
    double ratearray[] = {0.010, 0.015, 0.020, 0.025, 0.030};

    double amountarray[];
    amountarray = new double[19];


    for(int i=0; i<=3; i++)
    {
        for(int j=0; j<=5; j++){

            amountarray[k] = principal * Math.pow((1 + ratearray[j]), yearsarray[i]);

            System.out.println(" answer " + amountarray[k] );


    }
}

我需要另一个for循环来增加amountarray []中k的值吗?

我想拥有amountarray 的所有值,即amountarray[0]、amountarray[1]、amountarray[2]、.......等等。

提前感谢您的帮助。

4

4 回答 4

3
amountarray = new double[19];

上面的代码是假的,因为你需要有4x5 = 20双值

此代码将始终正常工作,您应该使用它,并且您需要学习编写如下代码:

public class Prin {
public static void main (String [] args){

    //...
    int[] yearsarray = {1,2,3,4};
    double[] ratearray = {0.010, 0.015, 0.020, 0.025, 0.030};
    double[] amountarray = new double[yearsarray.length * ratearray.length];

    int k = 0;

    for(int i=0; i<yearsarray.length; i++){
        for(int j=0; j<ratearray.length; j++){
            amountarray[k] = principal * Math.pow((1 + ratearray[j]), yearsarray[i]);
            System.out.println(" answer " + amountarray[k] );
            k++;
    }
}
于 2013-08-20T11:48:18.113 回答
2

“该程序现在将计算(年)n = 1,2,3,4 的最终金额 A,比率 r = 1%、1.5%、2%、2.5%、3%”

...这意味着您的答案以二维矩阵的形式出现。

因此,您amountarray需要定义为:

double amountarray[][] = new double[yearsarray.length][ratearray.length];

然后你会计算:

amountarray[i][j] = principal * Math.pow((1 + ratearray[j]), yearsarray[i]);
于 2013-08-20T11:44:34.477 回答
1

不,你没有

int k=0; // initialization
for(int i=0; i<=3; i++)
{
    for(int j=0; j<=5; j++){
        // use post-increment
        amountarray[k] = principal * Math.pow((1 + ratearray[j]), yearsarray[i]);
        System.out.println(" answer " + amountarray[k] );
        // now we can increment k
        k = k+1;
}

还有这个:因为你似乎使用 yearsarray 只是为了得到一个 i+1 的值,为什么不这样做

amountarray[k] = principal * Math.pow((1 + ratearray[j]), i+1);

这样你就可以摆脱yearsarray,至少在这种情况下

编辑:一个重新设计的版本,它还处理了一些其他小问题并减少了“幻数”的使用

public class Prin {

    public static void main(String[] args) {

        System.out.println("Please enter the Principal you wish to invest =>");
        Scanner stdio = new Scanner(System.in);
        int principal = stdio.nextInt();
        stdio.nextLine();

        System.out.println("This program will now calculate the final amount A for (years) n = 1,2,3,4 and the rate r = 1%, 1.5%, 2%, 2.5%, 3%");

        int yearsarray[] = {1, 2, 3, 4};
        double ratearray[] = {0.010, 0.015, 0.020, 0.025, 0.030};

        double amountarray[];
        // this way the array will follow the size of yearsarray and ratearray
        amountarray = new double[yearsarray.length * ratearray.length];


        int k = 0; // initialization
        for (int i = 0; i <= yearsarray.length; i++) {
            System.out.println("years=" + yearsarray[i]);
            for (int j = 0; j < ratearray.length; j++) {
                // use post-increment
                amountarray[k] = principal * Math.pow((1 + ratearray[j]), yearsarray[i]);
                System.out.println("  " + ratearray[j] + " answer " + amountarray[k]);
                k+=1;
            }
        }
    }
}
于 2013-08-20T11:42:56.270 回答
0

尝试将您的数据存储在地图中,这是您的循环的一个示例

 for(int i=0; i<=yearsarray.length; i++) {
     for(int j=0; j<=ratearray.length; j++) {
         double answer = (yearsarray, principal * Math.pow((1 + ratearray[j]), yearsarray[i]));
         Collection<Double> aux;
         if (!answers.containsKey(yearsarray[i])){
             aux = new ArrayList<Double>();         
             aux.add(answer);
         } else {
             aux = answers.get(yearsarray[i]);
             aux.add(answer);
         }
         answers.put(yearsarray[i], aux);
         // include the answers in a Map 
    }
}

谢谢

于 2013-08-20T11:54:18.130 回答