Results 1 to 5 of 5

Thread: create loop

  1. #1
    aconti is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default 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;

    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]
    [/CODE]

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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.

  3. #3
    Zman3359's Avatar
    Zman3359 is offline Member
    Join Date
    Jan 2011
    Posts
    42
    Rep Power
    0

    Default

    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.
    Java Code:
    if (1 + 1 == 2)
    {
        System.out.println("1 + 1 = 2");
    }
    System.out.println("Done!");
    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:
    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 bracket
    Last edited by Zman3359; 05-31-2011 at 10:54 AM.

  4. #4
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Cool

    Aconti,

    Start from here : Lesson: Language Basics

    Best Luck

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,778
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by aconti View Post
    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;

    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]
    [/CODE]
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Create loop in AWT
    By Hans. in forum AWT / Swing
    Replies: 0
    Last Post: 05-23-2011, 02:46 PM
  2. Replies: 26
    Last Post: 05-10-2011, 01:31 AM
  3. For loop to create objects
    By SteroidalPsycho in forum New To Java
    Replies: 4
    Last Post: 02-24-2010, 10:31 AM
  4. Replies: 2
    Last Post: 11-21-2008, 06:59 PM
  5. How do I create an array with every cycle of a loop?
    By blackhole8746 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:49 AM

Posting Permissions

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