Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Loop Structure

  1. #1
    Junior Member
    Join Date
    Aug 2017
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Loop Structure

    1) I want to need to write a program using a while loop structure that will display the following output:

    I have 1 sheep
    I have 2 sheep
    I have 3 sheep
    I have 4 sheep
    I have 5 sheep
    I have 6 sheep

    2) I also need to write a complete program that displays the multiples of 5 from 0 to 50. I also need to use a for loop structure in the program. The output should be as follows

    0 :Multiple of 5
    1
    2
    3
    4
    5 :Multiple of 5
    6
    7

    ......
    50 :Multiple of 5

  2. #2
    Member
    Join Date
    Aug 2017
    Location
    Northern Ireland
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop Structure

    Ok, we've established the requirements. What have you got so far? What part exactly are you stuck on?
    Tim Driven Development

  3. #3
    Junior Member
    Join Date
    Aug 2017
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop Structure

    I have done the I have 1 sheep java script like:
    class Sheep{
    public static void main(String[] args){
    for(int i=1; i<1; i++){
    System.out.println("I have 1 sheep: ");
    System.out.println("I have 2 sheep: ");
    System.out.println("I have 3 sheep: ");
    System.out.println("I have 4 sheep: ");
    System.out.println("I have 5 sheep: ");
    System.out.println("I have 6 sheep: ");
    }
    }
    }


    I also tried this code which works

    class Sheep {
    public static void main(String[] args){
    int[] numbers =
    {1,2,3,4,5,6};
    for (int item : numbers) {
    System.out.println("I have 1 sheep");
    }
    }
    }


    But I am stuck how to display Multiple of 5

    --- Update ---

    The word multiple of 5 must be displayed every 5 lines for example 0 : Multiple of 5 then 1 , 2, 3, 4 does not display it but 5 will display it

  4. #4
    Member
    Join Date
    Aug 2017
    Location
    Northern Ireland
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop Structure

    The first example is functionally correct but you have a loop that does one iteration, which is the same as no loop at all. The second example just prints "I have 1 sheep" six times.

    Consider some modification of the first example but have the loop iterate 6 times and make use of the 'i' variable in your output.
    Tim Driven Development

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Loop Structure

    Also please wrap your posted code in code tags:


    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Structure
    By Gerardgrundy in forum Object Oriented Programming
    Replies: 8
    Last Post: November 3rd, 2012, 02:25 AM
  2. Re: Data Structure
    By jim17 in forum Object Oriented Programming
    Replies: 3
    Last Post: November 16th, 2011, 11:14 PM
  3. Data Structure
    By Faha in forum Object Oriented Programming
    Replies: 9
    Last Post: November 10th, 2011, 01:35 AM
  4. nested loop structure
    By captianjaax in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 21st, 2011, 04:00 PM
  5. [SOLVED] Problems with Loop Structure
    By Sean137 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 13th, 2010, 02:59 PM