Results 1 to 6 of 6
  1. #1
    kewlgeye is offline Member
    Join Date
    Apr 2008
    Posts
    34
    Rep Power
    0

    Question Help combining loops into 1 program.

    Hello,

    I am having a difficult time with this. I have to take the 4 loops that I created and make 1 program out of all 4. For each loop that I have I need to use 1 of the following within the program:

    one value-returning method with a parameter (break loop)
    one value-returning method without a parameter (while loop)
    one void method with a parameter (do-while loop)
    one void method without a parameter (for loop)

    These will all be in 1 program. I have the loops I created below, but after reading my chapter twice I just don't comprehend how I am suppose to do this. Please help me with this. My loops are below.

    Java Code:
    import java.util.*;
    
    public class loop
    {
    
    public static void main (String[] args)
    {
    
    int i;
    
    
    for (i = 39; i >= 35; i = i - 2)
    
    
    	System.out.print(i + " ");
    
    for (i = 31; i >= 5; i = i - 2)
    
    
    	System.out.print(i + " ");
    
    
    for (i = 1; i >= 0; i = i - 2)
    
    
    	System.out.print(i + " ");
    
    
    System.out.println();
    }
    }
    Java Code:
    import java.util.*;
    
    public class loop
    {
    
    public static void main (String[] args){
    
    
    
    int count = 40;
    while (--count > 0){
    if (count % 2 == 0 || count == 33 || count == 3) continue;
    else System.out.print(count + " ");
    }
    }
    }
    Java Code:
    import java.util.*;
    
    public class loop
    {
    
    public static void main (String[] args){
    
    int loopcount = 31;
    do
    {
    loopcount--;
    if(loopcount == 27)
    continue;
    System.out.print(loopcount + " ");
    }
    
    while (loopcount > 5);
    
    }
    }
    Java Code:
    import java.util.*;
    
    public class loop
    {
    
    public static void main (String[] args){
    
    for(int i = 30; i > 0; i--)
    {
    if(i == 27)
    continue;
    else if(i < 5)
    break;
    else
    System.out.println(i);
    }
    }
    }
    Please help me combine these 4 loops into 1 program using the methods I described above.

    one value-returning method with a parameter (break loop)
    one value-returning method without a parameter (while loop)
    one void method with a parameter (do-while loop)
    one void method without a parameter (for loop)

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    This is 100% bad practice of coding actually. But for testing it's not bad.

    Java Code:
    public class LoopTest {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            System.out.println(new LoopTest().loopOne(0));
            
            System.out.println(new LoopTest().loopTwo());
    
            new LoopTest().loopThree(31);
            
            new LoopTest().loopFour();
        }
        
        private int loopOne(int j) {
            int retValue = j;
            int i;
            for (i = 39; i >= 35; i = i - 2)
                System.out.print(i + " ");
    
            for (i = 31; i >= 5; i = i - 2)
                System.out.print(i + " ");
    
            for (i = 1; i >= 0; i = i - 2)
                System.out.print(i + " ");
    
            System.out.println();     
            return retValue;
        }
        
        private int loopTwo() {
            int count = 40;
                while (--count > 0){
                    if (count % 2 == 0 || count == 33 || count == 3)
                        continue;
                    else 
                        System.out.print(count + " ");
                }
            return count;
        }
        
        private void loopThree(int loopcount) {
            do
            {
                loopcount--;
                if(loopcount == 27)
                    continue;
                System.out.print(loopcount + " ");
            }while (loopcount > 5);
        }
        
        private void loopFour() {
            for(int i = 30; i > 0; i--)
            {
                if(i == 27)
                    continue;
                else if(i < 5)
                    break;
                else
                    System.out.println(i);
            }
        }
    }
    Test one method at a time. And also I'm not clear what are the values you want to return.

    Try and see this sample code. If you like let me know what happened.

  4. #4
    kewlgeye is offline Member
    Join Date
    Apr 2008
    Posts
    34
    Rep Power
    0

    Smile Thank you Eranga

    This is the problem, I don't understand the values wanting to be returned either, except for the loops doing what they are already doing just using a void method for two, and the two value0returning methods which I believe are just suppose to be what is already being returned "the countdown"

    I tested the code, it works, I have to fix it up so that they print on 1 line for each loop, but this is not your fault, I think I coded print wrong for a couple of my loops. thats my fault.

    Thank you Eranga. :)

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes, you should give more details to do it well. Go through it and see whether you can understand my code.

    By the way, did you refer any book relating to this? Better to read more and get the idea about methods and stuff. :);)

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Question about loops
    By BHCluster in forum New To Java
    Replies: 4
    Last Post: 04-16-2008, 06:40 PM
  2. [SOLVED] Need help with Loops...please!
    By Zebra in forum New To Java
    Replies: 5
    Last Post: 04-10-2008, 02:44 PM
  3. Loops (while do etc)
    By manupr in forum New To Java
    Replies: 1
    Last Post: 01-15-2008, 04:59 AM
  4. Nested loops?
    By gabriel in forum New To Java
    Replies: 4
    Last Post: 08-06-2007, 05:51 PM
  5. Help me: loops in java
    By silvia in forum New To Java
    Replies: 3
    Last Post: 07-19-2007, 07:47 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
  •