lundi 29 février 2016

I cannot get my if statement to loop correctly

I am trying to prompt the user if the information they entered is correct and the program just proceeds anyway instead of re-prompting the user. I think i need to change how I loop this do some type of do-while loop, but I don't quite know what to do.

{

    //Here we will set up our method of data entry and decimal format
    Scanner keyboard = new Scanner(System.in);
    DecimalFormat eNotation1 = new DecimalFormat("00.00");

    //Let's introduce our program to the user
    System.out.println("Hi, This is a program to calculate quarterly interest growth");

    //Here we will prompt the user to enter the amount of quarters
    System.out.println("How many quarters would you like to display? Please pick a number greater than zero and less than or equal to 10");
    int quarterNumber = keyboard.nextInt();
    if (quarterNumber > 10 || quarterNumber < 0)
    {
    System.out.println("You entered a number outside of the boundrys. Please run the program again and choose a number between 0 and 10 (number can include 10)");
    System.exit(0);
    }

    //Here we will prompt the user for the starting balance.
    System.out.println("What is the starting balance of this account?");
    double startingBalance = keyboard.nextDouble();

    //Here we will prompt the user for the interest rate
    System.out.println("What is the interest rate (in percent) in your area? Please pick a number from 1 - 20.");
    double interestRate = keyboard.nextDouble();
    if (interestRate < 1 || interestRate > 20)
    {
    System.out.println("You entered a number outside of the boundrys. Please run the program again and choose a number between 1 and 20 (can include 1 and 20)");
    System.exit(0);
    }

    //Here we will double check with the user that the info is correct.
    System.out.println("The amount of quarters you would like displayed is " + quarterNumber);
    System.out.println("The starting balance you would like to work with is " + startingBalance);
    System.out.println("The interest rate in your area is " + interestRate);
    System.out.println("Is this information correct?");
    keyboard.nextLine();
    String answer = keyboard.nextLine();
    System.out.println("");

    //We will have them enter the information again if the answer is no or No
    if (answer == "no" || answer == "No")
    {
    //Here we will prompt the user to enter the amount of quarters
    System.out.println("How many quarters would you like to display? Please pick a number greater than zero and less than or equal to 10");
    keyboard.nextInt();
    quarterNumber = keyboard.nextInt();

    //Here we will prompt the user for the starting balance.
    System.out.println("What is the starting balance of this account?");
    keyboard.nextDouble();
    startingBalance = keyboard.nextDouble();

    //Here we will prompt the user for the interest rate
    System.out.println("What is the interest rate (in percent) in your area? Please pick a number from 1 - 20.");
    keyboard.nextDouble();
    interestRate = keyboard.nextDouble();
    }

    //Next we will proceed with the calculation if the information is indeed correct
    double endingBalance = startingBalance + (startingBalance * (interestRate / 100 * .25));
    double interestEarned = endingBalance - startingBalance;

    //Now we will output the information
    for (int qn = 1; qn < (quarterNumber + 1); qn++ , startingBalance = startingBalance + interestEarned , endingBalance =startingBalance + (startingBalance * (interestRate / 100 * .25)), interestEarned = endingBalance - startingBalance )
    {
    System.out.println("Quarter Number                      Starting Balance                      Interest Earned                           Ending Balance   ");
    System.out.println(qn + "                                   " + eNotation1.format(startingBalance) + "                                " + eNotation1.format(interestEarned) + "                                      " + eNotation1.format(endingBalance) + "   ");                
    }   


 }

}

Aucun commentaire:

Enregistrer un commentaire