Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: need help with this basic program containing loops

1. ## 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:

f4Plb.png

2. Wallis product:

OA5mM.jpg

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

3. ## 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) {

}

}