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:

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

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.