Asterisk Star pattern help
I already know how to do make the patterns
**********
*********
********
*******
******
*****
****
***
**
*
and
*
**
***
****
*****
******
*******
********
*********
**********
But im having trouble with patterns like these
http://img184.imageshack.us/my.php?i...6919698zu1.jpg
Asterisk Star pattern help
For these exercises, you need to work out the algorithm before you start thinking about code.
For the diamond shape, I noticed that if the maximum number of rows is even, the width will be max rows - 1. Then look at the pattern (for max row = 10):
Row - Spaces - Stars
1 ----- 4 ----- 1
2 ----- 3 ----- 3
3 ----- 2 ----- 5
4 ----- 1 ----- 7
5 ----- 0 ----- 9
6 ----- 0 ----- 9
7 ----- 1 ----- 7
8 ----- 2 ----- 5
9 ----- 3 ----- 3
10 ----- 4 ----- 1
This looks to me like it would be a loop counting by 2s (1, 3, 5, 7 ...). The loop counter would be the number of stars, the number of spaces is calculated. I have 2 loops, one counting up to max rows / 2, and one counting back down.
These loops would have other loops inside it (nested loops) to print the stars and asterisks.
The triangle is easier, it has a pattern like this (again for max row = 10):
Row - Spaces - Stars
0 ----- 0 ----- 10
1 ----- 1 ----- 9
2 ----- 2 ----- 8
3 ----- 3 ----- 7
4 ----- 4 ----- 6
etc.
You need a loop that will go max rows times, then nested loops to print spaces and stars. The number of spaces and stars are calculated based on what row you're printing.
I hope I'm being clear. I'm trying to give you hints without giving away everything. It appears this exercise is about loop control and nested loops. Don't be afraid to experiment and see what you get as a result. Java compile/executes are pretty quick for code this size.
Good luck,
alan
some functions for diff forms
Some functions allowing you such forms:
Code:
//StarsForms.java
public class StarsForms{
public static void main(String argv[]){
leftTriangle(1, 5, 0);
System.out.println("--------------------------------");
leftTriangle(4, 1, 0);
System.out.println("--------------------------------");
rightTriangle(2, 5, 0);
System.out.println("--------------------------------");
rightTriangle(5, 2, 0);
System.out.println("--------------------------------");
centerTriangle(1, 5, 0);
System.out.println("--------------------------------");
centerTriangle(1,-4, 1);
System.out.println("--------------------------------");
diamond(1, 9, 5);
System.out.println("--------------------------------");
diamond(2, 10, 5);
}
public static void leftTriangle(int firstrow, int lastrow, int trans){
if(firstrow < 0 || lastrow < 0)
return;
if(firstrow < lastrow){
for (int i = firstrow; i <= lastrow; i++) {
printNchars(trans, " ");
printNcharsln(i, "*");
}
}else{
for (int i = firstrow; i >=lastrow ; i--) {
printNchars(trans, " ");
printNcharsln(i, "*");
}
}
}
public static void rightTriangle(int firstrow, int lastrow, int trans){
if(firstrow < 0 || lastrow < 0)
return;
if(firstrow < lastrow){
for (int i = firstrow; i <= lastrow; i++) {
printNchars(trans, " ");
printNchars(lastrow-i, " ");
printNcharsln(i, "*");
}
}else{
for (int i = firstrow; i >=lastrow ; i--) {
printNchars(trans, " ");
printNchars(firstrow-i, " ");
printNcharsln(i, "*");
}
}
}
public static void centerTriangle(int minStars, int rows, int trans){
if(minStars < 0 || rows == 0)
return;
if(0 < rows){
for (int i = 0; i < rows; i++) {
printNchars(trans, " ");
printNchars(rows-i-1, " ");
printNcharsln((i*2)+minStars, "*");
}
}else{
rows = -rows;
for (int i = rows-1; i >= 0; i--) {
printNchars(trans, " ");
printNchars(rows-i-1, " ");
printNcharsln((i*2)+minStars, "*");
}
}
}
public static void diamond(int minStars, int rows, int trans){
if(minStars < 0 || rows == 0)
return;
centerTriangle(minStars, (rows+1)/2, 0 + trans);
centerTriangle(minStars,-rows/2, rows%2 + trans);
}
public static void printNchars(int n, String c){
for(int i = 0; i<n; i++)
System.out.print(c);
}
public static void printNcharsln(int n, String c){
for(int i = 0; i<n; i++)
System.out.print(c);
System.out.println();
}
}
I hope it can help you:)