Friday 15 July 2011

java - Printing an arrowhead of stars using nested for loops -



java - Printing an arrowhead of stars using nested for loops -

print arrowhead. allow user input how big arrow should entering total number of lines draw. user entered 9 in illustration below. prompt user come in odd number only. should model this:

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

so far code looks this:

int i, j, numrows; scanner reader=new scanner(system.in); system.out.println("how many rows triangle have?"); numrows=reader.nextint(); //row for(i=1;i<=numrows;i++){ //column for(j=1;j<=i;j++){ system.out.print("*"); } system.out.println(); } }

}

this top half. can create bottom half altering nested loop. know need have nested loop decrease amount of stars after reaches middle column, or (i/2)+1, not sure how this. tried using if statement j-- within of column loop either not right or made mistake.

put bottom half:

for(i=numrows;i>=0;i--){ for(j=i-1;j>0;j--){ system.out.print("*"); } system.out.println(); }

java

No comments:

Post a Comment