I'm new in Java and supposed to write a program that prints a number into a diamond shape. It doesn't print correctly, can someone point out where I'm messed up?

private static void diamondOfAsterisks(int number) {
		int spaces = number / 2;
		int stars = 1;
		int row;
		int column;
		int middle = number / 2;
		
		for(row = 0; row < number; row++){
			
		for( column = 0; column < spaces;  column = column +1){
				
			System.out.print(" ");		
		}
		for (column = 0; column < stars; column = column +1 ){
			System.out.print("*");
		}
		
		System.out.println("");
		
		if ( row < middle){
			spaces = spaces - 1;
			stars = stars + 1;
		}
		
		else{
			spaces = spaces + 1;
			stars = stars - 1;
		}
		
		}
		
		
	}

Recommended Answers

All 3 Replies

So the output for 5 should be something like this?

*
 * *
*   *
 * *
  *

How about for even numbers like 6? This?

**
 *  *
*    *
 *  *
  **

Are the diamonds to be filled or hollow?

Thanks for the reply . .

the diamond should be filled


*
**
***
**
*

the diamond shape isn't saving correctly for some reason . . .

Well thanks anyway but I got it solved. My problem was here

if ( row < middle){
			spaces = spaces - 1;
			stars = stars + 2;
		}
		
		else{
			spaces = spaces + 1;
			stars = stars - 2;
		}

I had stars + 1 and stars - 1 instead of 2

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.