# need help with this basic program containing loops

• March 23rd, 2012, 04:16 PM
kingslayer
need help with this basic program containing loops
Hey all, any help you could give me would be greatly appreciated. I'm pretty new and obviously very terrible at this, so thanks in advance.

These are the instructions I was given:

Write a Java program to calculate an approximation of π using the first n terms in the formulae given below:

Attachment 1131

2. Wallis product:

Attachment 1132

The program should have a main menu with three options:
2. Wallis product
3. Exit the program
The program should NOT exit unless the user enters ‘3’. Any other integer input that is not [1-3] should prompt the user to re-enter the correct number in the range.
If the user enters either 1 or 2, the program should ask for the number of terms n from the user (you are not expected to perform an error check on n, assume valid input). The program should display the n results of the series.
Important:
· The program should go back to the main menu after performing the calculations for either option 1 or 2.
· The program should inform the user if the number entered for menu selection outside the bounds of [1,3] and go back to the main menu (NOT exit!). Exit is on input 3 only.
Analyze the above formulas. Note that Wallis product calculates π/2 . Also, note that for Wallis product, the lower bound is 1 and for Madhava-Leibniz series, lower bound is 0. Upper bound for both will be user input, n.
Calculate the result by hand for a few n inputs to test your program – do not rely on the correctness of your program without actually verifying the output.
You should be using loops for the menu and for approximation calculations.

And some further instructions on making the menu
do {
System.out.println("Make a selection for the program to execute pi calculations: \n" +
"\t" + "1. Madhava-Leibniz Formula \n" +
"\t" + "2. Wallis Product \n" +
"\t" + "3. Exit the program");

//get user input
input = scanner.nextInt();

//one option is to set up an inner while loop here - "an error message loop"
//and loop until user gives you the input you want: 1, 2 or 3
//basically: until correct input is entered, do NOT allow to proceed
while(input not in [1 ,3] interval) {
// error message: Invalid input;
//solicit user input
}

//once you passed through the error checking loop, you can know that input is either 1, 2 or 3.

if(input == 1) {
}
else if(input == 2) {
//Wallis series - print feedback to screen
}

//quit when the user inputs 3 - since the input passed through error message loop, it is either 1, 2 or 3
//otherwise, if it was 1 or 2, the loop will send program control back to the top : the menu option
} while(input is NOT 3);

//print "exit" message to the user : Thank you for using the Pi Approximation Calculator
• March 23rd, 2012, 06:42 PM
Norm
Re: need help with this basic program containing loops
• March 23rd, 2012, 06:54 PM
kingslayer
Re: need help with this basic program containing loops
This is what I have so far, almost embarassed to post it

public class Approximation

{

public static void main(String [] args)

{

system.out.println("Make a selection for the program to execute pi calculations: \n" +
"\t" + "1. Madhava-Leibniz Formula \n" +
"\t" + "2. Wallis Product \n" +
"\t" + "3. Exit the program");
//get user input
input = scanner.nextInt();

while(input not in [1,3] interval)

{ system.out.println("Invalid input);
//solicit user input

}

if(input == 1) {

}

}