This week's book giveaway is in the OCAJP forum.We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!

# Can you tell what is wrong with this loop?

Shirl Diva
Greenhorn
Posts: 21
Can anyone explain what is going wrong with this code. It is supposed to calculate an infinite series of fractions to find the value of pi. The series begins with 4- 4/3, 4+ 4/5 and goes on alternating from + to -, incresing the denominator by 2 after each calculation. I have tried the for and the dowhile but can't seem to make it work. I am supposed to print output displaying the amount of times that I had to calculate in order to get to 3.14, 3.141, 3.1415, and 3.14159. I have not gotten very far because I am trying to first get the loop to work right. Any help!! Thanx

package TestBreak3;
import javax.swing.JOptionPane;
//import java.lang.Math;
import javax.swing.JTextArea;
public class TestBreak3 {
//Main method
public static void main(String[] args) {
double pi\$\$ = 0.00000;
double pi1\$ = 3.14159;//Math.PI;
double pi2\$ = 3.14;
double pi3\$ = 3.141;
double pi4\$ = 3.1415;
double divisor = 3;
double dividend = 4;
int count = 1;
JTextArea outputTextArea = new JTextArea();
outputTextArea.setText("Term\tValue of pi\n");
////////////////////////////////////////////////////////////
//while (pi\$\$ != 0){
//do {
//for(count = 1; pi\$\$ <= 100; count ++){
//for(;pi\$\$ <=3.14159; count++){
//////////////////////////////////////////////////////////////////////
pi\$\$ = 4 -(divisor/dividend);
divisor = divisor + 2;
count = count +=1;
outputTextArea.append(count + "\t" + pi\$\$ + "\n");
//}
//for(count += 1; pi\$\$ <= 100; count ++){
pi\$\$ = 4 +(divisor/dividend);
divisor = divisor + 2;
count = count +=1;
outputTextArea.append(count + "\t" + pi\$\$ + "\n");
//}
// }

[ November 06, 2002: Message edited by: Shirl Diva ]

Kelly Harris
Greenhorn
Posts: 22
I was not sure what you are trying to accomplish. Does this help?
kelly
public class TestBreak3
{
//Main method
public static void main(String[] args)
{
double pi\$\$ = 4.00000;
double pi1\$ = 3.14159;//Math.PI;
double pi2\$ = 3.14;
double pi3\$ = 3.141;
double pi4\$ = 3.1415;
double divisor = 3;
double dividend = 4;
int count = 1;
System.out.println("Term\tValue of pi\n");
////////////////////////////////////////////////////////////
while ( (pi\$\$ < pi1\$ || pi\$\$ > pi1\$) && (count < 10000) )
{
pi\$\$ -= (dividend/divisor);
if ( ( pi\$\$ >= pi2\$ && pi\$\$ < 3.15)
||( pi\$\$ >= pi3\$ && pi\$\$ <= 3.15)
||( pi\$\$ >= pi4\$ && pi\$\$ <= 3.15)
)
{
System.out.println(count + ":\t" + "-(" + dividend + "/" + divisor + ") =" + pi\$\$ + "\n");
}
divisor = divisor + 2;
count = count +=1;
pi\$\$ += (dividend/divisor);
if ( ( pi\$\$ >= pi2\$ && pi\$\$ <= 3.15)
||( pi\$\$ >= pi3\$ && pi\$\$ <= 3.15)
||( pi\$\$ >= pi4\$ && pi\$\$ <= 3.15)
)
{
System.out.println(count + ":\t" + "+(" + dividend + "/" + divisor + ") =" + pi\$\$ + "\n");
}
divisor = divisor + 2;
count = count +=1;
} // while
}
}

Bill Liteplo
Ranch Hand
Posts: 88
First of all, please put code in UBB
What are you trying to do? Increment the variable count? The simplest way is

(Do you really mean ?)
Second of all, in your lower (also commented out) for loop, you increment count in the loop definition, but also with a statement like the one above.
Bill

Bill Liteplo
Ranch Hand
Posts: 88
I would try to solve this in one for loop.
Something like:

It's a good idea to encapsulate functionality that is common and separable (e.g. checking the precision).
Hope this helps.
Bill