简体   繁体   中英

Returning a value to the main method

I am building a program that calculates and outputs values for: interest rates of 1% after 5 years for the values ranging from $1000-$20000 (1000, 2000, 3000, etc) with no compounding.

Ex) 1000 would be 1050, and 2000 would be 2100 (formula of A = P (1 + rt)).

At the current point, I am trying to write my code in such a way that when run, it lists all the values from 1000 - 20000 on different lines vertically, followed by all the values after the interest with no compounding takes affect:

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

(the above numbers are meant to appear vertically one below the other)

One of the items that I am trying to teach myself is the returning of a value from a method back to the main method. In this case, my calculateAmountNoCompounding method has a return value that I set to `double testValue = (j * (1 + .01 * NUMBER_OF_YEARS));. Yet when I try to return it to the main method by placing it in a print line function, I get the error ".class expected". I also get this same error on the actual return statement in line 25. Is there a simple and effective way to process this problem?

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;
}

}
  1. You don't call calculateAmountNoCompounding in main .
  2. double is a class here, so write it Double .
  3. You should init Double testValue; above the for loop so that it is visible when you want to return it.
  4. Calling an existing variable should be done without specifying the type. You can't do such return double testValue; , just do return testValue; . Because of 3. , Java already knows that testValue is a double. The same applies to System.out.println(double testValue); .

It should be return testValue; and not return double testValue . Also you need to declare the variable testValue outside the for loop

You do not need to have "double" before the print or return statements.

The return should just be testValue and the print either the value that was returned:

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

or can print directly

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

Okay so I don't really want to give you the answer to all of this. But I have used your code and put an example in there

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;
}

}

I hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM