将值返回到main方法

时间:2014-01-29 19:47:11

标签: java

我正在建立一个计算和输出以下值的程序:5年后1%的利率,价值范围从$ 1000- $ 20000(1000,2000,3000等),没有复合。

Ex)1000为1050,2000为2100(公式为A = P(1 + rt))。

目前,我试图以这样的方式编写我的代码:运行时,它会在垂直的不同行上列出1000 - 20000的所有值,然后在没有复合的兴趣之后列出所有值:

1000 2000 ... 20000 1050.0 2100.0 ... 21000.0

(上述数字意味着垂直显示在另一个之下)

我尝试自学的一个项目是将一个值从一个方法返回到main方法。在这种情况下,我的calculateAmountNoCompounding方法有一个返回值,我设置为`double testValue =(j *(1 + .01 * NUMBER_OF_YEARS));.然而,当我尝试通过将它放在打印行函数中将其返回到main方法时,我得到错误“.class expected”。我也在第25行的实际return语句中得到了同样的错误。是否有一种简单有效的方法来处理这个问题?

public class DepositCalculator {

public static final double ANNUAL_INTEREST_RATE = 1.0;
public static final int NUMBER_OF_YEARS = 5;
public static final int MONTHS_PER_YEAR = 12;
public static final int LOW_DEPOSIT_AMOUNT = 1000;
public static final int HIGH_DEPOSIT_AMOUNT = 20000;
public static final int DEPOSIT_AMOUNT_INCREMENT = 1000;

public static void main(String[] args) {

    for (int i = 0; i <= HIGH_DEPOSIT_AMOUNT - DEPOSIT_AMOUNT_INCREMENT; i = i + DEPOSIT_AMOUNT_INCREMENT) {
        System.out.println(LOW_DEPOSIT_AMOUNT + i);

    }     
    System.out.println(double testValue);

}

public static double calculateAmountNoCompounding(double deposit, double annualInterestRate, int numberOfYears) {

    for (double j = LOW_DEPOSIT_AMOUNT; j <= HIGH_DEPOSIT_AMOUNT - DEPOSIT_AMOUNT_INCREMENT; j = j + DEPOSIT_AMOUNT_INCREMENT) {
        double testValue = (j * (1 + .01 * NUMBER_OF_YEARS));
    }
    return double testValue;
}

}

4 个答案:

答案 0 :(得分:2)

  1. 您无法在calculateAmountNoCompounding中致电main
  2. double此处是一个类,因此请将其写为Double
  3. 您应该在Double testValue;循环之上初始化for,以便在您想要返回时显示它。
  4. 应该在不指定类型的情况下调用现有变量。你不能这样做return double testValue;,只做return testValue;。由于3.,Java已经知道testValue是double。这同样适用于System.out.println(double testValue);

答案 1 :(得分:0)

它应该是return testValue;而不是return double testValue。您还需要在for循环

之外声明变量testValue

答案 2 :(得分:0)

在打印或返回语句之前,您不需要使用“double”。

返回应该只是testValue并打印返回的值:

double answer = calculateAmountNoCompounding(...);
System.out.println(answer);

或者可以直接打印

System.out.println(calculateAmountNoCompounding(...));

答案 3 :(得分:0)

好的,所以我真的不想给你所有这些答案。但是我已经使用了你的代码并在那里放了一个例子

public class DepositCalculator {

public static final double ANNUAL_INTEREST_RATE = 1.0;
public static final int NUMBER_OF_YEARS = 5;
public static final int MONTHS_PER_YEAR = 12;
public static final int LOW_DEPOSIT_AMOUNT = 1000;
public static final int HIGH_DEPOSIT_AMOUNT = 20000;
public static final int DEPOSIT_AMOUNT_INCREMENT = 1000;

public static void main(String[] args) {

    double result = 0.0;
    double deposit = 0.0;
    double annualInterestRate = 0.0;
    int numberOfYears = 0;

    //  you need to think about this
    /*for (int i = 0; i <= HIGH_DEPOSIT_AMOUNT - DEPOSIT_AMOUNT_INCREMENT; i = i + DEPOSIT_AMOUNT_INCREMENT) {
      //  System.out.println(LOW_DEPOSIT_AMOUNT + i);

    }*/    

    //  you need to set the deposit amount, annualinterestrate and number of years

    //result = calculateAmountNoCompounding(deposit, annualInterestRate, numberOfYears);

    //  here is an example deposit is 5000, annual interest is 10 and numberofyears is 5
    result = calculateAmountNoCompounding(5000, 10, 5);



    System.out.println("result is " +result);
}

public static double calculateAmountNoCompounding(double deposit, double annualInterestRate, int numberOfYears) {
    double testValue = 0.0;
    //  you need to think about this
    //for (double j = LOW_DEPOSIT_AMOUNT; j <= HIGH_DEPOSIT_AMOUNT - DEPOSIT_AMOUNT_INCREMENT; j = j + DEPOSIT_AMOUNT_INCREMENT) {
        testValue = (deposit * (1 + .01 * numberOfYears));
    //}
    return testValue;
}

}

我希望有所帮助。