Java with * Print graphics-diamond, hollow diamond, triangle, etc. __java

Source: Internet
Author: User
Tags int size

such as dreams, such as Dreams, moon petal smoke heavy. -Li Cun Lanxu such as Dream Order ·

Output Diamond Textbook Example 1 My example 2 output the next half diamond 3 output one side Diamond 4 output hollow Diamond 5 output on the part Diamond 6 output the lower part diamond 2 output triangle 1 output right triangle 2 Output triangle 3 output 99 multiplication table

1. Output Diamond Textbook Examples

The diamond with the output row number n.

   *
  ***
 *****
*******
 *****
  ***
   *
public class Diamond2 {public static void main (string[] args) {print (7);//7-line Diamond
        public static void print (int size) {if (size% 2 = 0) {size++;//Calculate diamond size}
                for (int i = 0; i < SIZE/2 + 1; i++) {for (int j = SIZE/2 + 1; j > i + 1; j--) { System.out.print (""); The blank of the upper-left corner of the output} for (int j = 0; J < 2 * i + 1; j + +) {System.out.print ("*"); /output diamond upper half Edge} System.out.println (); newline} for (int i = SIZE/2 + 1; i < size; i++) {for (int j = 0; j < I-SIZE/2; j+ +) {System.out.print (""); Output diamond lower left corner blank} for (int j = 0; J < 2 * size-1-2 * i; j +) {System.out.pri NT ("*"); Output rhombic lower half Edge} System.out.println (); NewLine}}} 
1.1 My example

The diamond above is 7 lines. The upper section is 4 lines and the lower part is 3 lines.

The number of lines in the diamond is always odd, and the spaces are always incremented or decremented by the value "1", and the number of 2*n-1per line is always number n.
Number of rows equal to the maximum number of lines.

The diamond is divided into the upper and lower parts, and the number of rows is n.

Upper part: N/2+1 Line
Next section: N/2 line

/** * output a specific number of rows diamond * Title:diamond2 * Description: * Company: * @author Peng * @date 1:17: */public class Mydemo {public static void main (string[] args) {print (7);//output 7 lines of diamond} public s
        tatic void print (int size) {if (size% 2 = 0) {size++;//compute diamond size} int i;
        Int J; for (i = 0; i < SIZE/2 + 1; i++) {for (j = 0; J < size/2-I. j) {SYSTEM.OUT.PRI
            NT ("");
            for (j = 0; J < 2 * (i + 1)-1; j +) {System.out.print ("*");
        } System.out.println (); for (i = 0; i < SIZE/2. i++) {for (j = 0; J < i + 1; j + +) {System.out.prin
            T ("");
            for (j = 0; J < 2 * (SIZE/2-i)-1; j +) {System.out.print ("*");
        } System.out.println (); }
    }
}

   *
  ***
 *****
*******
 *****
  ***
   *
1.2 output next half diamond
***********
 *********
  *******
   *****
    ***
     *
/** *
 Output the next half of the Diamond
 * Title:diamond3
 * Description: 
 * Company: 
 * @author Peng
 * @date afternoon 1 : 15:23 */public
class Diamond3 {public
    static void Main (string[] args) {
        print (11);//Output * The lower half of the number 7 diamond
    } public

    static void print (int size) {
        if (size% 2 = 0) {
            size++;//Calculate diamond size
        }
        in t i;
        Int J;
        for (i=0;i<size/2+1;i++) {for (
            j=0;j<i;j++) {
                System.out.print ("");//Output Space
            } for
            (j=0;j <2* (size/2-i) +1;j++) {
                System.out.print ("*");//output *
            }
            System.out.println ()
        ;
    }
}}
1.3 Output Side diamond
*
***
*****
*******
*****
***
*
/** *
 Title:diamond4 * Description of the output side: * Company 
 : 
 * @author Peng
 * @date 1:22:15 c12/>*/public
class Diamond4 {public
    static void Main (string[] args) {
        print (7);//The output * number is 7 of the side diamond
    Public

    static void print (int size) {
        if (size% 2 = 0) {
            size++;//COMPUTE diamond size
        }
        int i;
        Int J;
        for (i=0;i<size/2+1;i++) {for (            
            j=0;j<2* (i+1) -1;j++) {
                System.out.print ("*");              
            }
            System.out.println ();
        }
        for (i=0;i<size/2;i++) {for (      
            j=0;j<2* (size/2-i) -1;j++) {
                System.out.print ("*");
            }
            System.out.println ();}}

1.4 Output Hollow Diamond
   *
  * *
 *   *
*     *
 *   *
  * *
   *

Program 1

/** * Title:diamond7 * Description: Output Hollow Diamond * @author Peng * @date pm 1:52:51/public class Diamond7 {public static void Main (string[] args) {print (one);//} public static void print (int size) {if size
        % 2 = = 0) {size++;//Compute Diamond Size} Boolean isfirstorend = false;
        int i;
        Int J;
            Part for (i = 0; i < SIZE/2 + 1; i++) {isfirstorend = false on the graph;
            for (j = 0; J < size/2-I; j + +) {System.out.print ("");
            } System.out.print ("*");
                for (j = 0; J < 2 * I-1; j + +) {System.out.print ("");
            Isfirstorend = true;
            } if (isfirstorend) {//Determine whether the first line System.out.print ("*");
        } System.out.println ();
            ///Graph section for (i = 0; i < SIZE/2; i++) {isfirstorend = false; for (j = 0;J < i + 1;
            J + +) {System.out.print ("");
            } System.out.print ("*");
                for (j = 0; J < 2 * (SIZE/2-i-1)-1; j +) {System.out.print ("");
            Isfirstorend = true;
            } if (isfirstorend) {//To determine whether the last line System.out.print ("*");
        } System.out.println ();
 }
    }
}

Program 2

/** * Title:diamond8 * Description: Output Hollow Diamond 2 * @author Peng * @date pm 1:52:51/public class Diamond8 {public static void Main (string[] args) {print (7);//} public static void print (int size) {if size
        % 2 = = 0) {size++;//compute diamond size} int i;
        Int J; for (i = 0; i < SIZE/2 + 1; i++) {for (j = 0; J < size/2-I. j) {SYSTEM.OUT.PR
            Int ("");
            } System.out.print ("*");              
            for (j = 0; J < 2 * I-1; j + +) {System.out.print ("");
            } if (i==0) {}//If the first line does not output the second * number else{System.out.print ("*");
        } System.out.println (); for (i = 0; i < SIZE/2. i++) {for (j = 0; J < i + 1; j + +) {System.out.pri
            NT ("");
            } System.out.print ("*"); for (j = 0; J < 2* (SIZE/2-i-1)-1;          
            J + +) {System.out.print ("");
            } if (i== (size/2-1)) {}//If the second * number else{System.out.print ("*") is not exported at the end of the row;
        } System.out.println ();
 }
    }
}
1.5 part diamond on output
     *
    * *
   *   *
  *     *
 *       *
***********
Package com.jsp;

/**
 * title:diamond9 
 * Description: Output Upper Hollow diamond 
 * @author Peng
 * @date pm 2:44:51/public
class DIAMOND9 {public
    static void Main (string[] args) {
        print (one); 
    }

    public static void print (int size) {
        if (size% 2 = 0) {
            size++;//COMPUTE diamond size
        }
        int i;
        Int J;
        Front with a blank line for
        (i = 0; i < SIZE/2; i++) {for

            (j = 0; J < size/2-I. j) {
                System.out.print ("" );
            }
            System.out.print ("*");
            for (j = 0; J < 2 * I-1; j + +) {
                System.out.print ("");              
            }
            if (i==0) {}
            else{
                System.out.print ("*");
            }
            System.out.println ();
        }
        Outputs the last line for
        (i=0;i<size;i++) {
            System.out.print ("*")
        ;
    }
}
1.6 The next part of the output diamond
*********** * * * * * * * * * * * 
/** * title:diamond10 Description: output Lower Hollow diamond * * @author Peng * @date pm 2:55:51/Public C
    Lass DIAMOND10 {public static void main (string[] args) {print (11); public static void print (int size) {if (size% 2 = 0) {size++;//calculate diamond size} I
        NT I;
        Int J;
        Outputs the first line for (i = 0; i < size; i++) {System.out.print ("*");
        } System.out.println (); Line with spaces followed for (i = 0; i < SIZE/2; i++) {for (j = 0; J < i + 1; + +) {Syste
            M.out.print ("");
            } System.out.print ("*");
            for (j = 0; J < 2 * (SIZE/2-1-i)-1; j +) {System.out.print ("");
            } if (i = = SIZE/2-1) {} else {System.out.print ("*");
        } System.out.println (); }
    }
}
2 Output Triangles 2.1 Output triangle on the right
*
**
***
****
*****
/** *
 Output Right triangle
 * Title:diamond5
 * Description: * Company 
 : 
 * @author Peng
 * @date 1:22:15 c12/>*/public
class Diamond5 {public
    static void Main (string[] args) {
        print (5);
    }

    public static void print (int size) {

        int i;
        Int J;
        for (i=0;i<size;i++) {for            
            (j=0;j<i+1;j++) {
                System.out.print ("*");              
            }
            System.out.println ();}}

2.2 Output triangle with left side
     *
    **
   ***
  ****
 *****
******
/**
 * Output left triangle
 * Title:diamond6
 * Description: 
 * Company: 
 * @author Peng
 * @date pm 1:22:15
 *
/public class Diamond6 {public
    static void Main (string[] args) {
        print (6);
    }

    public static void print (int size) {

        int i;
        Int J;
        for (i=0;i<size;i++) {for
            (j=0;j<size-i-1;j++) {
                System.out.print ("");
            }
            for (j=0;j<i+1;j++) {
                System.out.print ("*");              
            }
            System.out.println ();}}

3 output 99 multiplication table
1x1=1   
1x2=2   2x2=4   
1x3=3   2x3=6   3x3=9  1x4=4 2x4=8 3x4=12 4x4=16  
1x5=5   2x5=10  3x5=15  4x5=20  5x5=25   1x6=6
Related Article

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.