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: diamond pattern and loop

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post diamond pattern and loop

    Dear Friends
    I am trying to write a program to display a diamond pattern based on the user input size.
    The output must like this below :

    Please type an integer number: 4
    ********
    ***##***
    **####**
    *######*
    *######*
    **####**
    ***##***
    ********
    But when I wrote my Java program , it gives the out put below
    Please type an integer number:  4
    ****
    ***##
    **####
    *######
    **#######
    ***#####
    ****###
    And this is my Java program .
    import java.util.Scanner; 
    public class DiamondPattern2{
    	public static void main(String[] args){
    	Scanner keyboard = new Scanner(System.in);
     
    		System.out.print("Please type an integer number:  ");
    		int n = keyboard.nextInt();
     
    		for (int i = 1; i <= n; i++){
    			System.out.print("*");
          for (int j = 0; j < (n - i); j++)
    			System.out.print("*");
    		for (int j = 2; j <= i; j++)
    			System.out.print("#");
    		for (int k = 2; k < (i+1); k++)
    			System.out.print("#");
     
    			System.out.println();
    			}
     
     		for (int i = n - 1; i >= 1; i--){
    			System.out.print("*");
          for (int j = 0; j < (n - i); j++)
    			System.out.print("*");
    		for (int j = 0; j <= (i*2); j++)
    			System.out.print("#");
    		for (int k = n; k <= (i); k++)
    			System.out.print("#");
     
             System.out.println();
     
             }
     
          }
     
    	}

    I don't know how to continue to complete s on the other side and fix # . Anyone can fix my program ?
    Thanks
    Naif


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: diamond pattern and loop

    The first thing I would do is write a method that takes 2 parameters: a char to print and an int for how manyy times to print that char. Then your main method will need to know how many stars and hashes to print on each line and call the print character method as many times as needed per line.
    Improving the world one idiot at a time!

  3. The Following User Says Thank You to Junky For This Useful Post:

    Naif (October 31st, 2011)

  4. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: diamond pattern and loop

    Quote Originally Posted by Junky View Post
    The first thing I would do is write a method that takes 2 parameters: a char to print and an int for how manyy times to print that char. Then your main method will need to know how many stars and hashes to print on each line and call the print character method as many times as needed per line.
    Thanks for reply , could you help me more to add the codes? Please
    Regards

  5. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: diamond pattern and loop

    Yes!

    What help were you after? Would you like me to just write it for you?
    Improving the world one idiot at a time!

  6. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: diamond pattern and loop

    yes, could you help me with the first part ?
    regards

    Quote Originally Posted by Junky View Post
    Yes!

    What help were you after? Would you like me to just write it for you?

Similar Threads

  1. Regular Expression pattern - complex pattern syntax
    By SmartAndy in forum Algorithms & Recursion
    Replies: 3
    Last Post: June 7th, 2011, 04:40 AM
  2. Method to print a diamond
    By snowMac in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 8th, 2011, 04:18 AM
  3. asterisk forming diamond
    By cutee_eyeh in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 13th, 2010, 08:53 PM
  4. Stuck -- Loop Pattern
    By CodeNewb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 11th, 2010, 03:02 AM
  5. Is it possible to print a spade, club, heart, or diamond in java?
    By BC2210 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 2nd, 2009, 08:35 PM