There were two other variation which I completed but I am confused with this one:

Write a method for each triangle variation. Use nested for loops to draw the triangles.

Variation 3:

/** Precondition: width is an odd number >1*/
public void diamond (int width)
{

}

if width = 7, the output should be:

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

Dani AI

Generated

The sample wants the widest row printed twice, which is why many "standard" diamond solutions (one peak only) won't match. A clear way to guarantee the requested shape is to iterate rows from 0 through width (inclusive) and derive the star count from min(row, width - row). That yields the sequence 1, 3, 5, ..., width, width, ..., 5, 3, 1 and keeps the logic simple.

A compact, nested-loop Java implementation (uses StringBuilder rather than repeated string concatenation):

/** Precondition: width is an odd number > 1 */
public void diamond(int width) {
    for (int row = 0; row <= width; row++) {
        int k = Math.min(row, width - row);
        int stars = 2 * k + 1;
        int spaces = (width - stars) / 2;

        StringBuilder line = new StringBuilder();
        for (int s = 0; s < spaces; s++) line.append(' ');
        for (int t = 0; t < stars; t++) line.append('*');

        System.out.println(line.toString());
    }
}

Troubleshooting notes and variants:

  • If you see one middle row instead of two, your outer loop likely runs row < width instead of <= width, or your star formula uses width - 1 in the wrong places.
  • Use StringBuilder (or prefilled char[]) for performance; repeatedly using += inside loops is slow.
  • To get the more common single-peak diamond, print the top half up to the middle, then print the bottom half starting one row below the middle (or loop row = 0; row < width; row++ and adjust the min formula).
  • 's posted snippet produces the single-peak form; if the exercise requires the duplicate center, change the loop bounds or use the min(row, width-row) approach above. 's suggestion to split responsibilities (compute counts, print a row) is useful for clarity and testing.

Recommended Answers

All 2 Replies

Hope this can help you. I found it while surfing

/** Precondition: width is an odd number >1*/
public void diamond (int width)
{
	   int middlePoint = width/2;
	   int currentRow=0;
	   
	   for(int i=0; i<width ;i++){
	   
		   String spaces = "";
		   for(int j=0;j<Math.abs(middlePoint-currentRow);j++){
				spaces+=" ";
		   }
	 
		   String stars="*";
		   int noOfStars;
		   
		   if(currentRow <= middlePoint){
				noOfStars = currentRow;
		   }else{
				noOfStars = width-currentRow-1;
		   }     
			
		   for(int k=0;k<noOfStars;k++){
				stars+="**";      
		   }
						
		   System.out.println(spaces+stars );
		   
		   currentRow++;
	   
	   }
 

}

Try using multiple methods... and make sure your LOGIC makes sense (Write it out and make sure it makes sense before you go into coding). If you post your code I can help.

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.