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

I have to create a program that displays asterisks in a variety of patterns using for loops. These is one display patterns than is just killing me:

(S) = black space

**********
(S)*********
(S)(S)********
(S)(S)(S)*******
(S)(S)(S)(S)******
(S)(S)(S)(S)(S)*****
(S)(S)(S)(S)(S)(S)****
(S)(S)(S)(S)(S)(S)(S)***
(S)(S)(S)(S)(S)(S)(S)(S)**
(S)(S)(S)(S)(S)(S)(S)(S)(S)*

Sorry for the bad diagram but it wouldn't format properly. Thanks for your time.

2006-11-04 09:00:23 · 2 answers · asked by Anonymous in Computers & Internet Programming & Design

2 answers

Do a for loop for that goes 10 times.

Have an inner for loop that does the spaces.
Provide a variable to take the value of the top loop counter. Allow this to count down to 0, stop the loop when 0. Printing out a space everytime the loop is used.

Have a second inner loop, after the inner loop for the spaces, that is for the stars.
Provide another variable, or use the one from before. Make it equal the counter of the top loop.
This time you want the counter to count up to 10, stopping at 10.

End the top loop.

That should do it for you. I made it, but did not want to supply the source, just the instructions on how to make it.

2006-11-05 22:39:45 · answer #1 · answered by Mark aka jack573 7 · 0 0

I don't have a compiler with me, but give this a shot... should come close or work...

Here it is:
public void doWork() {
int spaceCount = 0;
for ( int x = 0; x < 10; x++, spaceCount++) {
for ( int y = 0; y < 10; y++) {
if ( spaceCount > y ) {
System.out.print(" ");
} else {
System.out.print("*");
}
}
System.out.println("");
}
}

2006-11-04 20:24:27 · answer #2 · answered by mchenryeddie 5 · 0 0

fedest.com, questions and answers