Print triangle (hollow, solid)
Package COM. arithmetic;/*** print triangle (hollow, solid) ** @ author administrator **/public class test_wzs19 {public static void main (string [] ARGs) {trianglesolid (7); // print the solid triangle trianglehollow (7 ); // print a hollow triangle}/*** print a solid triangle ** @ Param line * print the number of triangle rows */static void trianglesolid (INT line) {for (INT I = 1; I <= line; I ++) {// control the print space for (Int J = 1; j <= line-I; j ++) {system. out. print ("") ;}// control the print * For (int K = 1; k <= 2 * I-1; k ++) {system. out. print ("*");} system. out. println () ;}/ *** print hollow triangle ** @ Param line * print the number of triangle rows */static void trianglehollow (INT line) {// control the number of printed rows for (INT I = 1; I <= line; I ++) {// control the print space for (Int J = 1; j <= line-I; j ++) {system. out. print ("") ;}// control the print * For (int K = 1; k <= 2 * I-1; k ++) {// The first line, print all the last line * if (I = 1 | I = line) {system. out. print ("*");} else {// The first and last prints * Other print spaces if (k = 1 | K = 2 * I-1) {system. out. print ("*");} else {system. out. print ("") ;}} system. out. println ();}}}
Console output:
* *** ***** ******* ********* ************************ * * * * * * * * * * **************