Print multiple triangles in cycles and print triangles in cycles

Source: Internet
Author: User

Print multiple triangles in cycles and print triangles in cycles

Print a row *. It's easy to print.

 

 

The Code is as follows:

1 public class Work10_3 {2 3/** 4 * @ param args 5 */6 public static void main (String [] args) {7 // TODO Auto-generated method stub 8 int a = 0; 9 while (a <4) {10 int I = 0; 11 while (I <10) {12 System. out. print ("*"); // print * Do not wrap 13 I ++; 14} 15 System. out. println (""); // wrap 16 a ++; 17} 18} 19}

 

However, after thinking for a long time yesterday, I did not expect the image below. Today, I suddenly got inspiration.

The code is very simple, that is, I was so stupid that I couldn't even think of my head.

Print one row in the first line *, two rows in the second line *, and three rows in the third line *. In this way, the analysis finds the rule and defines a = 1. The outer loop prints several rows, define an I = 0,

Implement the inner loop printing *. When a = 1 is the first line. If you want the inner to print a *, the inner loop condition is I <1, so that a * is printed *, when a = 2, it is the second line.

Print two layers *, then the inner layer is I <2, so it is not difficult to see I <a, so the code is as follows:

1 public class Work10 {2/** 3 * @ param args 4 */5 public static void main (String [] args) {6 // TODO Auto-generated method stub 7 int a = 1; 8 while (a <25) {9 int I = 0; 10 while (I <) {11 System. out. print ("*"); // print * Do not wrap 12 I ++; 13} 14 System. out. println (""); // line feed 15 a ++; 16} 17} 18}

 

The code is simple and can be improved.

With an inspiration, you cannot waste it. You must fully train your talents,

So I printed another slash.

This is oblique to the right.

 

Print spaces on the inner layer (the same as printing on the inner layer *) and print a space on the outer layer *, which is slightly different from that on the outer layer.

The Code is as follows:

1 public class Work10_1 {2 3/** 4 * @ param args 5 */6 public static void main (String [] args) {7 // TODO Auto-generated method stub 8 int a = 1; 9 while (a <25) {10 int I = 0; 11 while (I <) {12 System. out. print (""); // print space without wrapping 13 I ++; 14} 15 System. out. print ("* \ n"); // print * and wrap 16 a ++; 17} 18} 19 20}

 

 

 

This is diagonal to the left. I define the inner layer I = 25, a = 1 to print 24 spaces, and then print * line breaks. When a = 2, 23 spaces are printed, then print * line breaks ..... The Code is as follows:

 

1 public class Work10_2 {2 3/** 4 * @ param args 5 */6 public static void main (String [] args) {7 // TODO Auto-generated method stub 8 int a = 1; 9 while (a <25) {10 int I = 25; 11 while (I>) {12 System. out. print (""); // print spaces without line breaks 13 I --; 14} 15 System. out. print ("* \ n"); // print * and wrap 16 a ++; 17} 18} 19 20}

 

There is nothing you can't think.

 

 

In the same way as above, one inner layer prints spaces while the other prints *,

This is also very simple. If I did not say this before, and now I know how to do it, I feel simple,

The Code is as follows:

 

1 public class Work10_4 {2 3/** 4 * @ param args 5 */6 public static void main (String [] args) {7 // TODO Auto-generated method stub 8 int a = 1; 9 while (a <25) {10 int I = 25; 11 while (I>) {12 System. out. print (""); // print space without wrapping 13 I --; 14} 15 int B = 0; 16 while (B <a) {17 System. out. print ("*"); // print * Do not wrap 18 B ++; 19} 20 System. out. println (""); // line feed 21 a ++; 22} 23} 24 25}

 

 

I did not expect this to be taken back. I changed the code. In fact, I want to create an isosceles triangle.

 

 

The Code is as follows:

 

1 public class Work10_5 {2 public static void main (String [] args) {3 // TODO Auto-generated method stub 4 int a = 0; 5 while (a <25) {6 int I = 25; 7 while (I> a) {8 System. out. print (""); // print spaces without wrapping 9 I --; 10} 11 int B = 0; 12 while (B <a) {13 System. out. print ("*"); 14 B + = 2; // here is different from the above 15} 16 System. out. println (""); 17 a ++; 18} 19} 20}

 

 

This isosceles triangle is not easy to do. I tried it a few times but did not do it. I want to define a variable and try it...

 

 

 

It turns out that you really need to add a variable, which will be made instantly after the variable is added.

 

 

For analysis, the first line prints a lot of spaces first, then a *, and the second line reduces space by one. * adds two because the increase speed is different, therefore, two variables are required to control the two inner loops. The printing of spaces is the same as that of the previous ones, but the speed of printing * is increased. The Code is as follows:

 

 1         // TODO Auto-generated method stub 2         int a=1; 3         int c=1; 4         while(a<25){ 5             int i=25; 6             while(i>a){ 7                 System.out.print(" "); 8                 i--; 9             }10             int b=0;11             while(b<c){12                 System.out.print("*");13                 b++;14             }15             System.out.println("");16             a++;17             c+=2;18         }19     

 

Then I want to print an inverted triangle and print it out easily.

 

 

The Code is as follows:

 

 1 public class Work10_6 { 2  3     /** 4      * @param args 5      */ 6     public static void main(String[] args) { 7         // TODO Auto-generated method stub 8         int a=1; 9         int c=1;10         while(a<25){11             int i=25;12             while(i>a){13                 System.out.print(" ");14                 i--;15             }16             int b=0;17             while(b<c){18                 System.out.print("*");19                 b++;20             }21             System.out.println("");22             a++;23             c+=2;24         }25     }26 27 }

This write triangle can also be combined in multiple ways. The following provides a reference:

The Code is as follows:

 

1 public class Work10_13 {2 3/** 4 * @ param args 5 */6 public static void main (String [] args) {7 // TODO Auto-generated method stub 8 int a = 1; 9 int c = 1; 10 while (a <20) {11 int I = 20; 12 while (I> a) {13 System. out. print (""); 14 I --; 15} 16 int B = 0; 17 while (B <c) {18 System. out. print ("*"); 19 B ++; 20} 21 System. out. println (""); 22 a ++; 23 c + = 2; 24} 25 int d = 0; 26 int e = 39; 27 while (d <20) {28 int I = 0; 29 while (I <d) {30 System. out. print (""); // print spaces without wrapping 31 I ++; 32} 33 int B = 0; 34 while (B <e) {35 System. out. print ("*"); 36 B ++; 37} 38 System. out. println (""); 39 d ++; 40 e-= 2; 41} 42} 43}

Certificate ------------------------------------------------------------------------------------------------------------------------------------------------------------

Reprinted please indicate the source!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.