Results 1 to 4 of 4
Thread: Need Help Creating A Loop!
- 05-12-2011, 05:46 PM #1
Member
- Join Date
- May 2011
- Posts
- 1
- Rep Power
- 0
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,
and this was the message after compiling: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
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
- 05-12-2011, 05:50 PM #2
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!
- 05-12-2011, 05:51 PM #3
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.
- 05-12-2011, 07:30 PM #4
Crossposted: Need Help Creating a Loop.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Similar Threads
-
Creating a loop help !
By theman279 in forum New To JavaReplies: 5Last Post: 03-10-2011, 10:13 AM -
creating variables in a for loop?
By sehudson in forum New To JavaReplies: 7Last Post: 03-01-2011, 03:37 AM -
Help with creating a while loop!
By BAD in forum New To JavaReplies: 1Last Post: 07-09-2010, 09:00 PM -
Creating a Loop
By SenorJalapeno in forum New To JavaReplies: 3Last Post: 04-02-2010, 10:13 AM -
Creating a New Method for Square Root Loop
By SapphireSpark in forum New To JavaReplies: 14Last Post: 02-25-2009, 01:21 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks