0

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:

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

3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by GTJava
0

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++;
	   
	   }
 

}
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.