Hi, everyone, can someone please take a look at my code and let me know why my do while loop does not work? The program runs properly until I add the do while loop. I referenced my book and have no idea how to do this differently.
Ashley
import java.util.Scanner; public class Project2 { public static void main(String[] args) { //variables double future = 0; double rate = 0; double numYears = 0; char runAgain; String input; System.out.println("This will find present value."); presentValue(future, rate, numYears); } public static double presentValue(double future, double rate, double numYears) { //creat a scanner object to read input. Scanner keyboard = new Scanner(System.in); //Get input from user do { System.out.println("What do you want the future value to be? "); future = keyboard.nextDouble(); System.out.println("What is the annual percentage rate? "); rate = keyboard.nextDouble(); System.out.println("How long will you keep your money in the account? (in years)"); numYears = keyboard.nextDouble(); //present = (future)/(1+rate)^numYears //variables double present = (future)/Math.pow(1+rate,numYears); System.out.println("The present value is " + present + " ."); System.out.println("Would you like to run the program again? (Enter 'y' for yes " + " or 'n' for no"); input = keyboard.nextLine(); repeat = input.charAt(0); } while (runAgain == 'y' || runAgain == 'Y'); return present; } }
I also tried adding the "while" part after the return statement and that doesn't change anything. Thank you in advance.
Ashley