Results 1 to 5 of 5
Thread: create loop
- 05-31-2011, 08:47 AM #1
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
create loop
Hello,
I want to make a loop and print until the value of the num is under 15, what is wrong with the below please as I cannot take the value of variable num to the loop method;
[/CODE]Java Code:public class class3 { public static void main(String args[]){ int num = 10; if (num < 15); num ++; loop(); } public static void loop(String args[]){ if (num < 15); System.out.println(num); num ++[CODE]
- 05-31-2011, 08:55 AM #2
Read about if statements.
Read about loops.
Read about passing parameters.
There are several errors in your code that suggests you do not understand the basics. Go back and read your book/tutorial/lecture notes and get a better understanding.
- 05-31-2011, 09:48 AM #3
Lots of problems. You should definitely go back and review the basics. One, there is no reason to pass a String array to your loop() method because you don't use it. A String array is passed to the main() method because Java requires the main() method to have that for it to be able to run. If you just put "public static void main()" Java would complain that it couldn't find a main method. There is a use for the array passed to the main method but you don't need it right now. Two, don't put a semi colon after an if statement. Three, things you want to be done inside an if go inside brackets { } so that anything inside the brackets will be executed if the conditional statement is true. That is why you don't put a semi-colon because if you do nothing will be executed no matter what the conditional statement evaluates to. So in this example "1 + 1 = 2" will be printed only if 1 + 1 is equal to two (1 + 1 == 2) and "Done!" will be printed no matter what.
EDIT: Also I forgot to mention that you need a semi-colon after the second num++ call so "num++;". You also need closing braces for the loop() method and class3, although they may have just gotten cut off.Java Code:if (1 + 1 == 2) { System.out.println("1 + 1 = 2"); } System.out.println("Done!");
Java Code:public class Test { // <- Opening class bracket public static void main(String args[]) { // <- Opening method bracket // Do stuff } // <- Closing method bracket public static void loop() { // <- Opening method bracket // Do some more stuff } // <- Closing method bracket } // <- Closing class bracketLast edited by Zman3359; 05-31-2011 at 09:54 AM.
- 05-31-2011, 11:33 AM #4
Aconti,
Start from here : Lesson: Language Basics
Best Luck.gif)
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 05-31-2011, 11:42 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
We'd better list what is right in your code; have you studied even the basics of the Java language because the shown code doesn't make any sense at all, e.g. naming a method explode() doesn't make it do as its name suggests. Go find a book and study a bit.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Create loop in AWT
By Hans. in forum AWT / SwingReplies: 0Last Post: 05-23-2011, 01:46 PM -
need to create loop to force user to input integer
By Tolks in forum New To JavaReplies: 26Last Post: 05-10-2011, 12:31 AM -
For loop to create objects
By SteroidalPsycho in forum New To JavaReplies: 4Last Post: 02-24-2010, 09:31 AM -
Need help - Create an application to loop through folder of images & insert to db
By kissiffer4 in forum SWT / JFaceReplies: 2Last Post: 11-21-2008, 05:59 PM -
How do I create an array with every cycle of a loop?
By blackhole8746 in forum New To JavaReplies: 3Last Post: 05-07-2008, 07:49 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks