aspose file tools*
The moose likes Beginning Java and the fly likes Can you tell what is wrong with this loop? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can you tell what is wrong with this loop?" Watch "Can you tell what is wrong with this loop?" New topic
Author

Can you tell what is wrong with this loop?

Shirl Diva
Greenhorn

Joined: Sep 09, 2002
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

Joined: Mar 27, 2002
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

Joined: Oct 16, 2002
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

Joined: Oct 16, 2002
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can you tell what is wrong with this loop?
 
Similar Threads
Problem with printf output
return three values from a while loop
Newbie problem - HELP!
Did I do this right? (Interest calculator)
Need help with doing a calculation in Java