Results 1 to 8 of 8

Thread: Help with loops

  1. #1
    ahanslot is offline Member
    Join Date
    Sep 2013
    Posts
    26
    Rep Power
    0

    Default Help with loops

    I have two questions how can I change the following code so it uses

    a) while loops rather than do loops
    b) using for loops

    Java Code:
    public class StarMethod3
    {
      private void stars(int numstars)
      {
        int line = 4;
        do
        {
          int stcount = numstars;
          do
          {      
            System.out.print("*");
            stcount--;
          }while (stcount > 0);
          System.out.println();
          line--;
        }while (line > 0);
      }
     
      public static void main(String args[])
      {
        StarMethod3 s = new StarMethod3();
        s.stars(2);
      }
    }

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

    Default Re: Help with loops

    What have you tried?
    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
    ahanslot is offline Member
    Join Date
    Sep 2013
    Posts
    26
    Rep Power
    0

    Default Re: Help with loops

    I've just tried re arranging the code I know that it should be like:

    while (true){
    // your code goes here
    }
    in the above format but I get errors?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Help with loops

    What errors?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    ahanslot is offline Member
    Join Date
    Sep 2013
    Posts
    26
    Rep Power
    0

    Default Re: Help with loops

    I'm not sure how to change do while to while loop so i tried:

    Java Code:
    public class StarMethod3
    {
      private void stars(int numstars)
      {
        int line = 4;
        while (int stcount = numstars);
        {
            System.out.print("*");
            stcount--;
          }
        while (stcount > 0);
          System.out.println();
          line--;
        }
      while (line > 0);
    }
     
      public static void main(String args[])
      {
        StarMethod3 s = new StarMethod3();
        s.stars(2);
      }
    }
    It wont compile, do I put the arguments in one line?

  6. #6
    ahanslot is offline Member
    Join Date
    Sep 2013
    Posts
    26
    Rep Power
    0

    Default Re: Help with loops

    I managed to do it using

    Java Code:
    public class StarMethod3
    {
      public void stars()
      {
        int line = 3;
        while (line > 0)
        {
          int stcount = 3;
          while (stcount > 0)
          {      
            System.out.print("*");
            stcount--;
          }
          System.out.println();
          line--;
        }
      }
    
      public static void main(String args[])
      {
        StarMethod3 s = new StarMethod3();
        s.stars();
      }
    }
    i just need help coverting using only for loop

  7. #7
    ahanslot is offline Member
    Join Date
    Sep 2013
    Posts
    26
    Rep Power
    0

    Default Re: Help with loops

    ive now managed to do with for loops using

    Java Code:
    //using for loops
    public class StarMethod3
    {
    
      private void print()
      {
       for (int count=4;count>0;count--)
          System.out.print("*");
        System.out.println();
      }
      public static void main(String args[])
      {
        StarMethod3 s = new StarMethod3();
        s.print();
       }
    }
    this prints out four stars how do i include 'line' so it prints out multiple lines?

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

    Default Re: Help with loops

    You have to slow down and really think about what you're trying to do. You can't just rearrange code and hope for the best. What are you trying to achieve? Break your problem down into smaller steps, and take them one step at a time.
    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

  1. Help with loops!
    By jrelvi23 in forum New To Java
    Replies: 9
    Last Post: 09-04-2012, 02:36 PM
  2. Replies: 20
    Last Post: 03-22-2011, 05:02 AM
  3. loops
    By curioustoknow in forum New To Java
    Replies: 3
    Last Post: 02-06-2011, 02:45 PM
  4. need some help with loops!
    By Chewart in forum New To Java
    Replies: 2
    Last Post: 12-04-2009, 12:32 AM
  5. these loops...
    By Blaedel in forum New To Java
    Replies: 0
    Last Post: 10-01-2009, 07:59 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
  •