Results 1 to 4 of 4
  1. #1
    chrisnojen117 is offline Member
    Join Date
    May 2011
    Posts
    1
    Rep Power
    0

    Default Need Help Creating A Loop!

    Hello, I am in the process of learning Java and having some troubles. The book I am reading gave me a scenario to create a program that calculates volume. Then repeats if yes, and stops if no... This is what I have finished so far though I am receieving errors when I compile it? I cant seem to figure out what im doing wrong. All help is greatly appreciated!

    here is the code,

    Java Code:
    /* Protoype program to be used to prove first concepts Algorithm project */
    
    import java.io.*;
    
    import java.lang.*;
    
    import java.text.*;
    
    import java.util.*;
    
    //Set up public class
    
    public class Document1
    
    {
    
    //declare main() method
    
    public static void main(String[] args) throws IOException
    
    {
    do
    {
    
    //Declare data types
    
    float length,width,height,volume,loop,yes,no;
    
    //set up input stream
    
    
    
    Scanner readin = new Scanner(System.in);
    
    System.out.print("\n\nCalculate the Volume by entering the height ,width, and length.\n\n\n");
    
    
    
    
    
    //prompt for length
    
    System.out.println("\n\n enter value for length");
    
    //stream in length and convert to float
    
    length = readin.nextFloat();
    
    //prompt for width
    
    System.out.println("\n\n enter value for width");
    
    //stream in width and convert to float
    
    width = readin.nextFloat();
    
    //prompt for height
    
    System.out.println("\n\n enter value for height");
    
    //stream in width and convert to float
    
    height = readin.nextFloat();
    
    
    
    
    //Calculate results
    
    volume = length*width*height;
    
    //prepare data for out put display
    
    DecimalFormat twodig = new DecimalFormat("########.##");
    
    System.out.print("\n\nThe results of the calcuations are\n\n\n");
    
    System.out.print("the Volume is:\t\t" + twodig.format(volume) + "\n\n");
    
    loop = readin.nextFloat();
    yes = 0;
    no = 1;
    
    //prompt for loop
    
    System.out.println("\n\n calculate again Yes / No ?");
    
    
    
    }
    while (loop = 0);
    
    //stream in loop and convert to float
    
    
    
    
    }//end main
    
    }//end class
    and this was the message after compiling:

    C:\Users\christian\Desktop\Document1.java:91: cannot find symbol
    symbol : variable loop
    location: class Document1
    while (loop = 0);
    ^
    1 error

    Tool completed with exit code 1

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default

    You've got some pretty wonky syntax and logic problems there. For starters, you check equality with == or .equals(), not =.

    But why don't you just read in a String and check that (using the equals() method) instead of using a float for some reason?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,884
    Rep Power
    25

    Default

    The compiler can't find the loop variable because it is out of scope where you use it. The scope of a variable is determined by the enclosing {}s. A variable defined inside of a pair {} is not known outside the enclosing {}.

    Also (loop = 0) is an assignment statement, Not a comparsion. Use == to compare.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

Similar Threads

  1. Creating a loop help !
    By theman279 in forum New To Java
    Replies: 5
    Last Post: 03-10-2011, 11:13 AM
  2. creating variables in a for loop?
    By sehudson in forum New To Java
    Replies: 7
    Last Post: 03-01-2011, 04:37 AM
  3. Help with creating a while loop!
    By BAD in forum New To Java
    Replies: 1
    Last Post: 07-09-2010, 10:00 PM
  4. Creating a Loop
    By SenorJalapeno in forum New To Java
    Replies: 3
    Last Post: 04-02-2010, 11:13 AM
  5. Creating a New Method for Square Root Loop
    By SapphireSpark in forum New To Java
    Replies: 14
    Last Post: 02-25-2009, 02:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •