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.