English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

hello,can anybody please show me how to do this:

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

2007-01-10 06:02:52 · 2 answers · asked by trickyme 1 in Computers & Internet Programming & Design

2 answers

public class Pyramid {

public static void main(String[] args) {

int i;
i = 1;
int j;
int depth = 4;

while( i < (depth * 2)) {

j = 1;
while( j <= (depth-Math.abs(i-depth))) {

System.out.print( "*");
j++;
}

System.out.println("");
i++;
}

}
}


You can also change

int depth = 4;

to

int depth = Integer.parseInt( args[ 0]);

to change the depth via the command-line. Just make sure you have a default value in case there are no arguments.

2007-01-10 07:35:57 · answer #1 · answered by Kookiemon 6 · 0 0

First, write a loop that will print n stars on a line. Use print() instead of the usual println() so the stars don't go on separate lines.

Do you have to use a while loop for this? Why not look at the for loop? It seems more natural.

Once you have that working, try putting your first loop inside another loop that increments the value of n from 1 to 4.

With this done, you should have a good understanding of how to finish up the assignment by using a second set of loops. Good luck.

2007-01-10 14:17:58 · answer #2 · answered by arbeit 4 · 0 0

fedest.com, questions and answers