Don't say anything, waiter. Code First:
(Figure I)
As shown in the code above, since the execution order of the program is from left to right, top down, so when we want to print in a DOS window a hollow diamond two is shown
(Figure II)
When: we can divide it into two parts,
In Figure one, we first create a scanner object that, through its nextint () method, receives a numeric value of type int (scanner Class) from the DOS window (see JAVAAPI for more information). The number is then assigned to the total, which is the total number of rows of the hollow diamond we want to print.
By total, the number of rows in the upper half of the half and the lower half of the row are given Halfmore respectively.
At this point, we are divided into two parts: (Take total=9 as an example)
Upper part:
At this time, the top half of the row number is 3 rows, first with an outer statement to control the number of lines to print, one in the ⑴ line of the statement.
The internal content of this loop:
Each of our loops is printed in order from left to right, from top to bottom in the print sequence.
In the process of printing involves three main elements: number of rows, spaces, asterisks;
A secondary feature: line wrapping.
First, because we let the outer loop control the "Number of rows," we will analyze the relationship between the number of spaces and the number of lines, the number of asterisks, and the number of lines, respectively:
Left space and number of lines:
Number of left spaces in nth row an
1 4
2 3
3 2
4 1
It can be seen that when the number of rows is increasing linearly, the left space is decreasing.
namely: a1 = 4;
A2 = 3;
a3 = 2;
a4 = 1; The general formula of arithmetic progression: an = a1 + (n-1) *d: an = 3 + ( -1) (n-1) = 3-n + 1.
That is: Left space = number of lines-the first few lines + 1.
The code for the ⑵ line in a For loop is printed.
The blank space on the left is printed with an asterisk followed by the number of intermediate spaces:
Median space and number of lines:
Number of middle spaces in nth row an
1 0
2 1
3 3
4 5
The same approach to the left-hand space is slightly different, except that the remainder of the first item is incremented.
Since we are using a for loop, the loop initialization condition is 1, so when our loop condition is negative, the number of prints is 0 times.
So we can set the first item to 1, then there: an = 1 + (n-1) * 2 = 2 * n-3. This has the code for the ⑶ line in Figure one.
The ⑷ line is used to print the asterisk on the right, except for the first line, and finally, the outermost control line number of loops is changed once per rotation.
The second part: The second half of the analysis of the idea is to follow the upper half of the opposite print.
Finally, be sure to remember that when programming:
Thinking must always be clear!
Thinking must always be clear!
Thinking must always be clear!
A detailed analysis of the use of the For loop and if statements in Java to print a hollow diamond (or even by re-combing it to deepen your understanding!)