Javase's Drawing Diamond

Source: Internet
Author: User

In the Javase algorithm exercise, drawing a diamond is a more common case. The diamond effect looks like this:

When we solve algorithmic problems, we usually do not want to code immediately, but first observe, find out the key to solve the problem.

In, we can see that the number of rows and columns of the diamond can be folded along the horizontal or vertical axis, so the number of rows and columns of the diamond should be odd;

The number of spaces in the upper left corner of the diamond, from the top of the horizontal axis, descending, in particular, each line decrements a space, until the horizontal axis no space;

The top and bottom of the diamond have an asterisk, each row has two asterisks between the top and bottom, and in the upper part of the diamond, the asterisk of each line is the beginning of the line (the corner mark is 0), and the angle is twice times the number of the row;

The upper part of the diamond is folded in half along the horizontal axis to obtain the lower half of the diamond, thus completing the diamond drawing.

After we have done the above analysis of the algorithm, the following code is implemented. See the following code:

Import java.util.scanner;/*** Draw Diamond * @author Li Zhangyong **/public class Demo {public static void main (string[] args) {Scanner Sc=ne W Scanner (system.in);//Create a keyboard input object System.out.println ("Please enter an integer:");//Prompt for int size=sc.nextint ();//Enter an integer, and assign a value to the size variable sc.close ();//Close input stream if (size%2==0) {//judgment if not odd, add 1 to odd size+=1;} Draw System.out.print ("") in the upper-left corner of the top half of the diamond for the (int i=0;i<=size/2;i++) {for (int j=size/2;j>i;j--) {///Diamond) ("");} if (j==0 | | j==2*i) {System.out.print ("*") for the j=0;j<=2*i;j++ (int) {///diamond) with the asterisk and the Space ("*");} Else{system.out.print ("");}} System.out.println ();//Draw a line}//start to draw the lower half of the diamond, int k=0;for (int i=size/2+1;i<size;i++) {k+=2;//because it is folded along the horizontal axis, Therefore, in the lower part of the diamond, the line adjacent to the diamond horizontal axis is folded in half of the two rows of the diamond line, and so on (int j=0;j<i-size/2;j++) {System.out.print ("");} for (int j=0;j<=2* (i-k); j + +) {if (j==0 | | j==2* (I-K)) {System.out.print ("*");} Else{system.out.print ("");}} System.out.println ();}}}

Running the above code shows the following effects:

Javase to draw the diamond

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.