PHP exercise-print hollow diamond with For Loop

Source: Internet
Author: User

Train of Thought: 1. WriteCodePrint the solid pyramid

2. modify the code and empty the pyramid.

3. modify the code and reverse the pyramid

4. Modify the reversed code and empty the pyramid.

5. modify the code and parameterize the specific number.

1. Write the code to print the pyramid of solid gold:

<? PHP  //  $ N = 5;      For ( $ I = 1; $ I <= 5; $ I ++ ){  //  Print space          For ( $ J = 1; $ J <= 5- $ I ; $ J ++ ){  Echo "& Nbsp ;" ;}  //  Print *          For ( $ K = 1; $ K <= 2 * $ I -1; $ K ++){  Echo "*" ;}  Echo "<Br/>" ;} ?>

 

2,

2. Improve code and empty the pyramid

<? PHP  //  $ N = 5;      For ( $ I = 1; $ I <= 5; $ I ++ ){ //  Print space          For ( $ J = 1; $ J <= 5- $ I ; $ J ++ ){  Echo "& Nbsp ;" ;}  //  Print *      For ( $ K = 1; $ K <= 2 *$ I -1; $ K ++ ){  //  Print the first line and the last line are connected *          If ( $ I = 1 | $ I = 5 ){  Echo "*" ;}  Else  {  //  How can I enter the number and *?             If ( $ K = 1 | $ K = 2 * $ I -1 ){  Echo "*" ;}  Else  {  Echo "& Nbsp ;" ;}}}  Echo "<Br/>" ;} ?>

 

3. modify the code and reverse the pyramid

<? PHP  //  $ N = 5;      For ( $ I = 1; $ I <= 5; $ I ++ ){  //  Print space          For ( $ J = 1; $ J <= 5- $ I ;$ J ++ ){  Echo "& Nbsp ;" ;}  //  Print *          For ( $ K = 1; $ K <= 2 * $ I -1; $ K ++ ){  //  Print the first line and the last line are connected *              If ( $ I = 1 | $ I = 5 ){  Echo "*" ;}  Else  {  //  How can I enter the number and *?                  If ( $ K = 1 | $ K = 2 * $ I -1 ){ Echo "*" ;}  Else  {  Echo "& Nbsp ;" ;}}}  Echo "<Br/>" ;}  //  Reverse      For ( $ I = 5; $ I > = 0; $ I --){  //  Print space          For ( $ J = 0; $ J <= 5- $ I ; $ J ++ ){  Echo "& Nbsp ;" ;}  //  Print *          For ( $ K = 1;$ K <= 2 * $ I -3; $ K ++ ){  Echo "*" ;}  Echo "<Br/>" ;} ?>

 

4. Modify the reversed code, empty the pyramid, and remove the * sign in the middle to modify the code (if ($ I = 1 | $ I = 5) {// remove $ I = 5 and empty the center)

<? PHP  //  $ N = 5;      For ($ I = 1; $ I <= 5; $ I ++ ){  //  Print space          For ( $ J = 1; $ J <= 5- $ I ; $ J ++ ){  Echo "& Nbsp ;" ;} //  Print *          For ( $ K = 1; $ K <= 2 * $ I -1; $ K ++ ){  //  Print the last line after the first line and connect * ($ I = 1 | $ I = 5)              If ( $ I = 1 ){ //  Remove $ I = 5 and empty the middle.                  Echo "*";}  Else  {  //  How can I enter the number and *?                  If ( $ K = 1 | $ K = 2 * $ I -1 ){  Echo "*" ;}  Else  {  Echo "& Nbsp ;" ;}}}  Echo "<Br/>" ;}  //  Reverse Blank      For ( $ I = 5; $ I > = 0; $ I -- ){  //  Print space          For ( $ J = 0;$ J <= 5- $ I ; $ J ++ ){  Echo "& Nbsp ;" ;}  //  Print *          For ( $ K = 1; $ K <= 2 * $ I -3; $ K ++ ){ //  How can I enter the number and *?              If ( $ K = 2 * $ I -3 | $ K = 1 ){  Echo "*" ;}  Else  {  Echo "& Nbsp ;" ;}}  Echo "<Br/>";} ?>

 

5. modify the code and parameterize the specific number: Change the specific number 5 to $ n = 5, and then set the value of $ n to zoom in or out.

<? PHP  $ N = 5 ;  For ( $ I = 1; $ I <= $ N ; $ I ++ ){  //  Print space          For ($ J = 1; $ J <= $ N - $ I ; $ J ++ ){  Echo "& Nbsp ;" ;}  //  Print *          For ( $ K = 1; $ K <= 2 * $ I -1;$ K ++ ){  //  Print the last line after the first line and connect * ($ I = 1 | $ I = 5)              If ( $ I = 1 ){ //  Remove $ I = 5 and empty the middle.                  Echo "*" ;}  Else  {  //  How can I enter the number and *?                  If ($ K = 1 | $ K = 2 * $ I -1 ){  Echo "*" ;}  Else  {  Echo "& Nbsp ;" ;}}}  Echo "<Br/>" ;}  //  Reverse Blank     For ( $ I = $ N ; $ I > = 0; $ I -- ){  //  Print space          For ( $ J = 0; $ J <= $ N - $ I ; $ J ++){  Echo "& Nbsp ;" ;}  //  Print *          For ( $ K = 1; $ K <= 2 * $ I -3; $ K ++ ){  //  How can I enter the number and *?              If ( $ K = 2 * $ I -3 | $ K = 1 ){  Echo "*" ;}  Else  {  Echo "& Nbsp ;" ;}}  Echo "<Br/>" ;} ?>

 

:

the final success is achieved. By the way, programming is not difficult, but it is difficult to find a solution to the problem. Therefore, everyone is good at thinking in the programming process. Although it took me half a day to think about it, I finally came up with it and solved the problem. So I usually need to practice it with my brains. Our programming capability can be improved. Only by practice can things be understood and knowledge points be mastered.

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.