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 13 of 13

Thread: Display patterns using loops

  1. #1
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    268
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Exclamation Display patterns using loops

    Used nested loops that display following patterns

                    *
                   ***
                  *****
                   ***
                    *

    I only able to have this, after that stucked.
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j <= i * 2; j++) {
             System.out.print("*");
    	}
             System.out.println();
    	}
    Last edited by John Joe; January 17th, 2018 at 11:59 AM. Reason: change QUOTE to CODE
    Whatever you are, be a good one

  2. #2
    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: Display patterns using loops

    Can you post the program's current output to show what it does?

    Describe the pattern of the output:
    how many lines are printed?
    what goes on each line?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    268
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Display patterns using loops

    Current output is
    *
    ***
    *****
    *******
    *********
    Last edited by John Joe; January 17th, 2018 at 12:00 PM. Reason: change QUOTE to CODE
    Whatever you are, be a good one

  4. #4
    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: Display patterns using loops

    Can you describe the pattern of the desired output:
    how many lines are printed?
    what goes on each line?
    How does what is printed on a line relate to what line is being printed?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    268
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Display patterns using loops

    Thanks for your reply Norm, I actually want it to print a diamond.
    I have solved it.

         for (int i = 1; i < 5; i += 2) {
    			for (int j = 0; j < 4 - i / 2; j++)
    			   System.out.print(" ");
     
    			for (int j = 0; j < i; j++)
    			  System.out.print("*");
     
    			System.out.println();
    		}
     
    		for (int i = 5; i > 0; i -= 2) {
    			for (int j = 0; j < 4 - i / 2; j++)
    				System.out.print(" ");
     
    			for (int j = 0; j < i; j++)
    				System.out.print("*");
     
    			System.out.println();
    		}
    Whatever you are, be a good one

  6. #6
    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: Display patterns using loops

    want it to print a diamond.
    I have solved it.
    You could Wrap the post of the output in code tags to preserve the leading spaces so we can see its shape.

    Glad you solved it.

    Note: The code has too many "magic numbers". Can you change it so the number of lines to print is in a variable and then the contents of that variable is used to control the logic?
    Then make it a method and execute it with different values.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    268
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Display patterns using loops

    You could Wrap the post of the output in code tags to preserve the leading spaces so we can see its shape.
    This is the output
       * 
      * * 
     * * * 
      * * 
       *
    What did you mean "magic numbers" ?
    Whatever you are, be a good one

  8. #8
    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: Display patterns using loops

    All the 5s and 4s should be replaced by variables. The 2 is ok because it is used to get half of a value and would never change.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    268
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Display patterns using loops

    Don't understand Did you mean it would be better if I assign a variable to 4 and 5 ?
    Whatever you are, be a good one

  10. #10
    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: Display patterns using loops

    Replace the numbers 4 and 5 with variables. Don't hard code numbers in the code.
    Then write a method that takes the number of rows as its arg.
    Call the method with different values for the number of rows.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    268
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Lightbulb Re: Display patterns using loops

    I think you mean this ?

    public class Example {
     
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		System.out.print("Key in the row you want it to display: ");
    		int k = input.nextInt();
    		Example x = new Example();
    		x.getPatternDisplay(k);
    	}
     
    	public void getPatternDisplay(int k) {
     
    		for (int i = 1; i < k; i += 2) {
    			for (int j = 0; j < k - i / 2; j++)
    				System.out.print(" ");
    			for (int j = 0; j < i; j++)
    				System.out.print("*");
    			System.out.println();
    		}
     
    		for (int i = k; i > 0; i -= 2) {
    			for (int j = 0; j < k - i / 2; j++)
    				System.out.print(" ");
    			for (int j = 0; j < i; j++)
    				System.out.print("*");
    			System.out.println();
    		}
    	}
    }
    Whatever you are, be a good one

  12. #12
    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: Display patterns using loops

    Yes, something like that. The variable name k is a bit obscure: k??? instead of nbrOfRows that describes what is in it.
    If you don't understand my answer, don't ignore it, ask a question.

  13. The Following User Says Thank You to Norm For This Useful Post:

    John Joe (January 18th, 2018)

  14. #13
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    268
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Display patterns using loops

    Thanks, I will aware of the variables used next time
    Whatever you are, be a good one

Similar Threads

  1. Patterns!! Please help!
    By winn1me in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 14th, 2013, 05:49 PM
  2. Design Patterns
    By keepStriving in forum Java Theory & Questions
    Replies: 3
    Last Post: October 29th, 2013, 11:22 AM
  3. Re: how to display a bar chart using loops
    By dwheeler in forum Loops & Control Statements
    Replies: 6
    Last Post: September 21st, 2013, 05:28 PM
  4. how to display a bar chart using loops
    By tirashad in forum Loops & Control Statements
    Replies: 5
    Last Post: August 11th, 2013, 12:59 AM
  5. Display patterns on the screen
    By finjws in forum Loops & Control Statements
    Replies: 12
    Last Post: February 10th, 2012, 01:11 PM

Tags for this Thread