PHP exercises-print hollow diamond with for Loop
Idea: 1. Write the code and print 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 spaces 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 spaces 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 * if ($ I = 1 | $ I = 5) {echo "*";} else {// question of how to empty the space 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 spaces 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 * if ($ I = 1 | $ I = 5) {echo "*";} else {// question of how to empty the space 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 spaces 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 echo "*";} else {// question of how to empty the space and * if ($ k = 1 | $ k = 2 * $ i-1) {echo "*";} else {echo "& nbsp;" ;}}} echo "<br/>" ;}// reverse the 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 to fill in the blank and * question 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 spaces 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 echo "*";} else {// question of how to empty the space and * if ($ k = 1 | $ k = 2 * $ i-1) {echo "*";} else {echo "& nbsp;" ;}}} echo "<br/>" ;}// reverse the 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 ++) {// Question of how to empty the space and * ($ 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.