Public classTest { Public Static voidMain (string[] args) {//number of input linesPrint (5); } Public Static voidPrintintsum) { intNumber=0; for(inti=1;i<=sum;i++) { intEmptynumber=sum-i;//calculate the number of spaces calculated as total rows-Number of lines intStarnumber=2*i-1;//Calculates the number of stars, calculated as twice times the number of rows-1 if(i==sum) { number=i+starnumber;//save a total for the back of the inverted output with } for(intz=0;z<emptynumber;z++)//traverse the output to get a triangle{System.out.print (" "); } for(intj = 0; J < Starnumber; J + +) {System.out.print ("*"); } System.out.println (); } //start outputting the following section intnewrow=sum+1;//calculate the number of lines to start intSumrow=2*sum-1;//calculate new Total rows for(inti=newrow;i<=sumrow;i++) { intEmptynumber=i-sum;//Calculates the number of spaces, calculated as the current number of rows-the old row count//Calculates the number of stars, the number of stars + spaces + The number of rows =number, which is the maximum number of triangles + its number of rows intstarnumber=number-i-Emptynumber; //Traverse Output for(intz=0;z<emptynumber;z++) {System.out.print (" "); } for(intj = 0; J < Starnumber; J + +) {System.out.print ("*"); } System.out.println (); } } }
Result diagram:
Print a prism in Java