Topic:
Print out the case (diamond)
Program Analysis:
First, the graph is divided into two parts to see, the first four lines a rule, after three lines a rule, the use of double for loop, the first layer control row, the second Level control column.
Code section:
1 Public classprog18{2 Public Static voidMain (string[] args) {3 intn = 5;4 Printstar (n);5 }6 //Print stars7 Private Static voidPrintstar (intN) {8 //print top Half9 for(inti=0;i<n;i++){Ten for(intj=0;j<2*n;j++){ One if(j<n-i) ASystem.out.print (""); - if(J>=n-i && j<=n+i) -System.out.print ("*"); the } - System.out.println (); - } - //print the lower half of the section + for(inti=1;i<n;i++){ -System.out.print (""); + for(intj=0;j<2*n-i;j++){ A if(j<i) atSystem.out.print (""); - if(J>=i && j<2*n-i-1) -System.out.print ("*"); - } - System.out.println (); - } in } -}
Operation Result:
* *** ***** ******* ********* ******* ***** *** *
Java Print out the case (diamond)