1-Print isosceles triangle
1 Public classDengyao {2 3 Public Static voidMain (string[] args) {4 for(inti=1;i<=5;i++) {//i--Control Line5 for(intj=5-i;j>=0;j--) {//j--Control the number of spaces6System.out.print ("");7 }8 for(intk=1;k<=2*i-1;k++) {//Number of k--control *9System.out.print ("*");Ten } OneSystem.out.println ();//Wrap one line at a time per loop A } - } -}
2-Print Right triangle
1 Public classZhijiao {2 3 Public Static voidMain (string[] args) {4 for(inti=1;i<=5;i++){5 for(intk=1;k<=i;k++){6System.out.print ("*");7 }8 System.out.println ();9 }Ten } One}
3-Print a solid diamond
1 Public classlingxing {2 3 Public Static voidMain (string[] args) {4 //First, the diamond as up and down, five next four, so the first for has 5 times, the second for4 times5 for(inti=1;i<=5;i++)6 {7 //The space and * separate, see "" Change i=1 when he is 4, 2 is 3 to find the law8 for(intj=1;j<=5-i;j++)9System.out.print ("");Ten for(intk=1;k<=2*i-1;k++)//find the rule, I is 1 3 5 7 cardinality OneSystem.out.print (' * ')); A //Change Line - System.out.println (); - } the for(inti=1;i<=4;i++) - { - for(intj=1;j<=i;j++)//Spaces 1 2 3 4 so -System.out.print (""); + for(intk=7;k>=2*i-1;k--)//* 7 5 3 1 Inverted base -System.out.print (' * ')); + System.out.println (); A } at}
4-Print Hollow diamond
1 Public classkongxinlingxing {2 3 Public Static voidMain (string[] args) {4 intnum = 5;5 for(inti=1;i<=num;i++){6 for(intj=1;j<=num-i;j++){7System.out.print ("");8 }9 for(intk=1;k<=2*i-1;k++){Ten if(k==1| | K==2*i-1){ OneSystem.out.print ("*"); A } - Else{ -System.out.print (""); the } - } - System.out.println (); - } + - for(inti=1;i<num;i++){ + for(intj=1;j<=i;j++) ASystem.out.print (""); at intCount = (num-i)-1; - for(intk=0;k<count;k++) { - if(k==0| | K==count-1) -System.out.print ("*"); - Else -System.out.print (""); in } -System.out.println (""); to } +}
Java Basics Loop Application (print triangles, diamonds)