import java.util.Scanner;
public class PiApprox {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
double n, i;
double x = 0;
do {
System.out.println("Please enter the ending value (the greater this is, the more accurate the approximation will be): ");
i = keyboard.nextDouble();
for (n = 0 ; n <= i ; n++ )
x += ((Math.pow(-1,n)) / ((2 * n) + 1));
System.out.println("The approximated value of pi is: " + (4 * x));
System.out.println("Do you want to run this program again? [y/n]: ");
char y = keyboard.next().charAt(0);
if (y == 'n' || y == 'N')
break;
} while (true);
}
}
the task indicates the we must:
Prompt the user for an ending value
Approximate the value of pi and display the answer
Ask the user if they want to try again, and do so accordingly.
I've got most of it; however, when i run it again, the formula gets messed up, and instead of an answer along hte lines of 3.14... i get 6(and a bunch of decimals). I cant understand why, so help would be greatly apprecaited. Thanks a bunch
This post has been edited by jon.kiparsky: 04 October 2012 - 07:05 PM

New Topic/Question
Reply




MultiQuote






|