3
for(int i = 1; i < 12; i++)
{
        for(int k=11; k > i; k--)
        {
                System.out.print("*");

        }
        System.out.print("\n");
}

I have the code above which displays a design like this:

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

I am wanting to swap it so that it looks like this:

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

I know that I need to use a loop and System.out.print(" ") in some way to leave spaces.

Whats the best approach to use? I created two separate loops, but with the next line commands this wont work in two loops. How would I integrate that into one loop?

3
  • 1
    What about printing spaces, then n-(the number of spaces you printed) asterisks? Commented Jan 22, 2012 at 1:06
  • I forgot to add, I can only use System.out.print("*"), System.out.print(" ") and system.out.print("\n"). Commented Jan 22, 2012 at 1:08
  • Since this is homework, incorporate another valuable concept in programming: functions. Write a function that prints a character n times, for example, repeat(char c, int times) { ... }. Then, for each row, you will be calling the function once for spaces, and once for asterisks. Commented Jan 22, 2012 at 1:20

3 Answers 3

2

For the second "picture": you'll have to print a variable number of spaces before the "*". That can be accomplished by using another loop before the loop that prints the "*", and knowing that every time you print a line, the number of spaces printed is incremented by one, and the number of asterisks is decremented by one.

EDIT :

Here's a hint, to get you started. Fill-in the blanks (and remove the comments):

int delta = /*fill*/;
for (int i = 0; i < /*fill*/; i++) {
    for (int j = 0; j < /*fill*/; j++) {
        System.out.print(" ");
    }
    for (int j = 0; j < /*fill*/; j++) {
        System.out.print("*");
    }
    delta += /*fill*/;
    System.out.print("\n");
}
Sign up to request clarification or add additional context in comments.

13 Comments

After printing the spaces, is there any command to go back to the top?
No, you don't go back to the top there, first you print the spaces, then the asterisks, then the line break - and that completes a single line. The outermost loop will take you "back to the top" for you to deal with the next line.
I like the concept of filling gaps ;)
@jjczopek I had no choice, I don't want to ruin the OP's homework :)
So in the code I have above, would i put the added loop inside the first loop or inside of the second loop?
|
0

Use String.format to left pad your string with spaces.

2 Comments

The OP said that he can only use System.out.print(), so String.format() is out of the question.
Sure, but that information was included in the comments, not the actual question.
-1

Do it using a 2-D array!!! Its very simple and efficient that way!!

For the second result:- Put '*' in places if row_no <= column_no (the half matrix above diagonal plus the diagonal elements) else put a space " " in other places. Run this in loops for row and column. Then print the 2-D array row wise.

1 Comment

You can use code blocks to indicate a piece of code. The formatting is also very nice. You can use them by surrounding the text with backticks ( ` ).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.