Java Pyramids and transformations

Source: Internet
Author: User

Java pyramids

Package Com.tfj.test;public class Jinzita {public static void main (string[] args) {int num=4;for (int i=0;i<num;i++) {//  Find out the space law for (int j=1;j<num-i;j++) System.out.print ("");//        Find out * rule for      (int k=0;k<2*i+1;k++) {    System.out.print ("*");      }    System.out.println ();}}}
Run results
<strong><span style= "FONT-SIZE:18PX;" ></span></strong><pre name= "code" class= "java" ><span style= "font-size:18px;" >java Pyramid deformation 1:</span>

<pre name= "code" class= "Java" >package Com.tfj.test;public class JinZiTa2 {public static void main (string[] args) { int num=4;for (int i=0;i<num;i++) {for (int j=1;j<num-i;j++) System.out.print ("");//  Method 1: Special handling Pyramid inside hollow// for      (int k=0;k<2*i+1;k++) {//       if (i>0&&i<num-1&&k>0&&k<2*i) {//       System.out.print ("");//       }       else{//     System.out.print ("*");//      }//     }//Method 2: First and last layer is processed, then the middle num-2 layer for (int k=0;k<2*i+1; k++) {if (i==0| | i==num-1) {System.out.print ("*");} else{if (k==0| | K==2*i) {System.out.print ("*");} else{System.out.print ("");     }} System.out.println ();}}}
Run results
<strong><span style= "FONT-SIZE:18PX;" >java Pyramid deformation 2:</span></strong>
<pre name= "code" class= "Java" >package Com.tfj.test;public class JinZiTa3 {public static void main (string[] args) { int num=4;for (int i=0;i<num;i++) {for (int j=1;j<num-i;j++) System.out.print ("");      for (int k=0;k<2*i+1;k++) {       if (i>0&&i<num&&k>0&&k<2*i) {       System.out.print ("");       }       else{     System.out.print ("*");      }     }     System.out.println ();} Invert the above and then handle the for (int i=num-1;i>0;i--) {for (int j=num-i;j>0;j--) System.out.print ("") slightly;      for (int k=2*i-1;k>0;k--) {       if (i>0&&i<num&&k>1&&k<2*i-1) {       System.out.print ("");       }       else{     System.out.print ("*");      }     }     System.out.println ();}}}
Run results






Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Pyramids and transformations

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.