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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: For loop with equation?

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default For loop with equation?

    Alright, so I received an assignment from my teacher to make a program that uses if and for loops to allow the reader to pick to calculate the circumference and area of a circle. The teacher said it needs to list the values with the radius increasing in a for loop from 1 to 20. I have only gotten so far as to create the circle with one radius because the looping is not working (supposedly because the radius has to be plugged into an equation). I have tried to switch some code around and add more, however none of my fixes seem to make much difference. I have hit a brick wall so could somebody please help me?

    // *****: This is program lists 20 circumfrence values of a circle and 20 area values of a circle.  Comments are in green text.
    // Syntax error = typing error - Does not compile
    // Logical error flaw is your reasoning they do compile.
    // Runtime error does compile but the program crashes when it runs. (Divide by zero)
    // Import some code
     
    import TerminalIO.KeyboardReader; //This is a piece of code to read characters from the keyboard.
    import java.text.DecimalFormat; //This piece of code is imported so the twoDecimal value will work.
     
    class Circle {//This class name matches the file name. 
     
     
    	public static void main(String args[]) { //Must have this line in all programs.
     
     
     
    	// decimals = double
     
     
    	KeyboardReader reader = new KeyboardReader();// Declares an object called reader that takes the form of Keyboard reader.
     
    	DecimalFormat twoDecimal = new DecimalFormat("0.00");  //Declares how many decimals that is created.
     
    	int i;
     
    	double circumference, area, pi, circletype;
     
    		System.out.println("Enter the value of Pi.");
     
    		pi = reader.readDouble(); //Finds pi
     
    		System.out.println("To find the circumfrence of a circle type 1. To find the area of a circle type 2.");
     
    		circletype = reader.readDouble(); //Reader decides to find the area or the circumfrence
     
    			if(circletype == 1){
     
    				for (i=1; i<=20; i=i+1);  //Loop
     
    					circumference = (2 * pi * i);
     
    			System.out.println("The circumference of a circle with a radius from 1 to 20 is, "+ circumference); //Loop of 20 different values in increasing order
     
    			}
     
     
     
    	}
    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: For loop with equation?

    Use braces with the for loop. And remove the semicolon at the end of the line with "for" in it. Ie all your for loops should look like

    for(/* etc */) {
        // code here
    }

    If your code is not giving the output you expect or intend, say what it *does* do and under what conditions.

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    UPYoda (January 31st, 2013)

  4. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: For loop with equation?

    Wow thanks I hadn't realized how small the error was. Thanks!

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: For loop with equation?

    You're welcome. The semicolon "ends" the for loop which is saying "for <whatever> do nothing". Then the other statements execute outside the loop.

Similar Threads

  1. Using an Array in an equation
    By georger55 in forum Java Theory & Questions
    Replies: 5
    Last Post: July 1st, 2012, 05:50 PM
  2. Java Equation correct syntax
    By macko in forum What's Wrong With My Code?
    Replies: 18
    Last Post: November 4th, 2011, 11:16 PM
  3. quadratic equation solver help!
    By overlord in forum Algorithms & Recursion
    Replies: 2
    Last Post: October 20th, 2011, 11:39 AM
  4. Testing equation of a line
    By TimoElPrimo in forum Object Oriented Programming
    Replies: 8
    Last Post: February 22nd, 2011, 11:40 PM
  5. Linear Equation Help !!!
    By thangavel in forum Algorithms & Recursion
    Replies: 1
    Last Post: January 13th, 2011, 05:32 AM

Tags for this Thread