Results 1 to 19 of 19
Thread: Pattern printing in Java
- 03-10-2011, 03:00 PM #1
Member
- Join Date
- Mar 2011
- Location
- West Bengal
- Posts
- 29
- Rep Power
- 0
- 03-10-2011, 03:06 PM #2
Member
- Join Date
- Feb 2011
- Posts
- 24
- Rep Power
- 0
prolly something like thisJava Code:System.out.println("*******"); System.out.println(" * * "); System.out.println(" * * "); System.out.println(" * "); System.out.println(" * * "); System.out.println(" * * "); System.out.println("*******");
- 03-10-2011, 03:10 PM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
Cross posted: Loops & Control Statements
- 03-10-2011, 03:17 PM #4
Member
- Join Date
- Mar 2011
- Location
- West Bengal
- Posts
- 29
- Rep Power
- 0
- 03-11-2011, 12:21 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
What have you tried?
- 03-11-2011, 11:39 AM #6
Member
- Join Date
- Mar 2011
- Location
- West Bengal
- Posts
- 29
- Rep Power
- 0
- 03-11-2011, 11:50 AM #7
Member
- Join Date
- Feb 2011
- Posts
- 43
- Rep Power
- 0
Try something in the lines of:
for() {}
While() {}
- 03-11-2011, 11:57 AM #8
Member
- Join Date
- Mar 2011
- Location
- West Bengal
- Posts
- 29
- Rep Power
- 0
- 03-11-2011, 12:01 PM #9
Member
- Join Date
- Feb 2011
- Posts
- 43
- Rep Power
- 0
No problem sir. Let's see the loop you've got so far. :)
- 03-11-2011, 12:08 PM #10
Member
- Join Date
- Mar 2011
- Location
- West Bengal
- Posts
- 29
- Rep Power
- 0
- 03-11-2011, 12:36 PM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Try thinking about it by the numbers. Look for a pattern in how the stars change.
- 03-11-2011, 02:42 PM #12
Member
- Join Date
- Feb 2011
- Posts
- 43
- Rep Power
- 0
Pseudo code, just coding from my head. You should be able to get something out of it.
Java Code://if user is going to decide size, use a scanner control size % 2 = 1 // I think, uneven anyway. loop i from 0 to size if(i == 0 && i == size) //print whole line else loop j from 0 to size if(j == i && j == size - i) print * else print ' '
- 03-11-2011, 05:25 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Why not use a bit of 101 mathematics? Your figure exists in a square of size n times n; think of two axes ranging from -n/2 to n/2. A star must be drawn at a position (x,y) iff:
1) y == n/2 or y == -n/2
2) y == x or y == -x
A simple method can implement this:
All you have to do is loop over the coordinates and print stars if they have to be printed:Java Code:boolean isStar(int x, int y, int n) { return Math.abs(y) == n/2 || Math.abs(x) == Math.abs(y); }
et voila ...Java Code:for (int y= -n/2; y <= n/2; y++) { for (int x= -n/2; x <= n/2; x++) System.out.print(isStar(x, y, n)?'*':' '); System.out.println(); }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-01-2011, 10:10 PM #14
Member
- Join Date
- Jul 2011
- Posts
- 2
- Rep Power
- 0
Re: Pattern printing in Java
import java.io.*;
class netpat
{
public static void main(String arg[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("How many * in first row ? : ");
int n=Integer.parseInt(br.readLine());
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i==1 || i==n)
System.out.print("*");
else
if(i==j || i==n-j+1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
this should give you your desired output....
- 07-01-2011, 10:57 PM #15
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Don't spoon feed code. When posting code, use code tags. Also, this thread is a few months old and I'd imagine to op is either long gone, or at least solved the problem already.
- 07-03-2011, 11:56 AM #16
Member
- Join Date
- Jul 2011
- Posts
- 2
- Rep Power
- 0
- 07-04-2011, 07:30 AM #17
- 07-04-2011, 07:34 AM #18
Sheesh.No probs.i need it using loopsdbJava Code:for (int i = 0; i < 1; i++) { System.out.println("*******"); System.out.println(" * * "); System.out.println(" * * "); System.out.println(" * "); System.out.println(" * * "); System.out.println(" * * "); System.out.println("*******"); }
- 07-04-2011, 07:35 AM #19
Similar Threads
-
strategy pattern and bridge pattern
By jomypgeorge in forum New To JavaReplies: 2Last Post: 12-13-2010, 05:13 AM -
Need to print pattern in java
By Gan in forum New To JavaReplies: 1Last Post: 10-20-2010, 08:36 AM -
Class pattern to generate following pattern:-
By vxs in forum New To JavaReplies: 5Last Post: 07-14-2010, 11:15 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks