Ideas: 1. Write the code to print solid pyramids
2. Modify the code and short the pyramid
3. Modify the code and turn the pyramid upside down
4. Modify the inverted code and empty the pyramid
5. Modify the code and parameterize specific numbers
1. Write the code to print the pyramid with solid gold characters:
<?<span>php </span><span>//</span><span>$n=5;</span> <span>for</span>(<span>$i</span>=1;<span>$i</span><=5;<span>$i</span>++<span>){ </span><span>//</span><span>打印空格</span> <span>for</span>(<span>$j</span>=1;<span>$j</span><=5-<span>$i</span>;<span>$j</span>++<span>){ </span><span>echo</span> " "<span>; } </span><span>//</span><span>打印*号</span> <span>for</span>(<span>$k</span>=1;<span>$k</span><=2*<span>$i</span>-1;<span>$k</span>++<span>){ </span><span>echo</span> "*"<span>; } </span><span>echo</span> "<br/>"<span>; } </span>?>
2,
2. Improve the code and empty the pyramid
<?<span>php </span><span>//</span><span>$n=5;</span> <span>for</span>(<span>$i</span>=1;<span>$i</span><=5;<span>$i</span>++<span>){ </span><span>//</span><span>打印空格</span> <span>for</span>(<span>$j</span>=1;<span>$j</span><=5-<span>$i</span>;<span>$j</span>++<span>){ </span><span>echo</span> " "<span>; } </span><span>//</span><span>打印*号</span> <span>for</span>(<span>$k</span>=1;<span>$k</span><=2*<span>$i</span>-1;<span>$k</span>++<span>){ </span><span>//</span><span>打印第一行后最后一行都打*连接</span> <span>if</span>(<span>$i</span>==1 || <span>$i</span>==5<span>){ </span><span>echo</span> "*"<span>; }</span><span>else</span><span>{ </span><span>//</span><span>怎么打空格和*号的问题</span> <span>if</span>(<span>$k</span>==1 || <span>$k</span>==2*<span>$i</span>-1<span>){ </span><span>echo</span> "*"<span>; }</span><span>else</span><span>{ </span><span>echo</span> " "<span>; } } } </span><span>echo</span> "<br/>"<span>; } </span>?>
3. Modify the code and turn the pyramid upside down
<?<span>php </span><span>//</span><span>$n=5;</span> <span>for</span>(<span>$i</span>=1;<span>$i</span><=5;<span>$i</span>++<span>){ </span><span>//</span><span>打印空格</span> <span>for</span>(<span>$j</span>=1;<span>$j</span><=5-<span>$i</span>;<span>$j</span>++<span>){ </span><span>echo</span> " "<span>; } </span><span>//</span><span>打印*号</span> <span>for</span>(<span>$k</span>=1;<span>$k</span><=2*<span>$i</span>-1;<span>$k</span>++<span>){ </span><span>//</span><span>打印第一行后最后一行都打*连接</span> <span>if</span>(<span>$i</span>==1 || <span>$i</span>==5<span>){ </span><span>echo</span> "*"<span>; }</span><span>else</span><span>{ </span><span>//</span><span>怎么打空格和*号的问题</span> <span>if</span>(<span>$k</span>==1 || <span>$k</span>==2*<span>$i</span>-1<span>){ </span><span>echo</span> "*"<span>; }</span><span>else</span><span>{ </span><span>echo</span> " "<span>; } } } </span><span>echo</span> "<br/>"<span>; } </span><span>//</span><span>倒转</span> <span>for</span>(<span>$i</span>=5;<span>$i</span>>=0;<span>$i</span>--<span>){ </span><span>//</span><span>打印空格</span> <span>for</span>(<span>$j</span>=0;<span>$j</span><=5-<span>$i</span>;<span>$j</span>++<span>){ </span><span>echo</span> " "<span>; } </span><span>//</span><span>打印*号</span> <span>for</span>(<span>$k</span>=1;<span>$k</span><=2*<span>$i</span>-3;<span>$k</span>++<span>){ </span><span>echo</span> "*"<span>; } </span><span>echo</span> "<br/>"<span>; } </span>?>
4. Modify the inverted code, empty the pyramid, and remove the * sign in the middle. Modify the code here (if($i==1 || $i==5){ //Remove $i= =5 and sell the middle short)
<?<span>php </span><span>//</span><span>$n=5;</span> <span>for</span>(<span>$i</span>=1;<span>$i</span><=5;<span>$i</span>++<span>){ </span><span>//</span><span>打印空格</span> <span>for</span>(<span>$j</span>=1;<span>$j</span><=5-<span>$i</span>;<span>$j</span>++<span>){ </span><span>echo</span> " "<span>; } </span><span>//</span><span>打印*号</span> <span>for</span>(<span>$k</span>=1;<span>$k</span><=2*<span>$i</span>-1;<span>$k</span>++<span>){ </span><span>//</span><span>打印第一行后最后一行都打*连接($i==1 || $i==5) </span> <span>if</span>(<span>$i</span>==1){ <span>//</span><span>去掉$i==5 把中间抛空</span> <span>echo</span> "*"<span>; }</span><span>else</span><span>{ </span><span>//</span><span>怎么打空格和*号的问题</span> <span>if</span>(<span>$k</span>==1 || <span>$k</span>==2*<span>$i</span>-1<span>){ </span><span>echo</span> "*"<span>; }</span><span>else</span><span>{ </span><span>echo</span> " "<span>; } } } </span><span>echo</span> "<br/>"<span>; } </span><span>//</span><span>倒转 抛空</span> <span>for</span>(<span>$i</span>=5;<span>$i</span>>=0;<span>$i</span>--<span>){ </span><span>//</span><span>打印空格</span> <span>for</span>(<span>$j</span>=0;<span>$j</span><=5-<span>$i</span>;<span>$j</span>++<span>){ </span><span>echo</span> " "<span>; } </span><span>//</span><span>打印*号</span> <span>for</span>(<span>$k</span>=1;<span>$k</span><=2*<span>$i</span>-3;<span>$k</span>++<span>){ </span><span>//</span><span>怎么打空格和*号的问题</span> <span>if</span>(<span>$k</span>==2*<span>$i</span>-3 || <span>$k</span>==1<span>){ </span><span>echo</span> "*"<span>; }</span><span>else</span><span>{ </span><span>echo</span> " "<span>; } } </span><span>echo</span> "<br/>"<span>; } </span>?>
5. Modify the code and parameterize the specific number: change the specific number 5 to $n=5 instead, and then set the value of $n, which can be enlarged or reduced at will
<?<span>php </span><span>$n</span>=5<span>; </span><span>for</span>(<span>$i</span>=1;<span>$i</span><=<span>$n</span>;<span>$i</span>++<span>){ </span><span>//</span><span>打印空格</span> <span>for</span>(<span>$j</span>=1;<span>$j</span><=<span>$n</span>-<span>$i</span>;<span>$j</span>++<span>){ </span><span>echo</span> " "<span>; } </span><span>//</span><span>打印*号</span> <span>for</span>(<span>$k</span>=1;<span>$k</span><=2*<span>$i</span>-1;<span>$k</span>++<span>){ </span><span>//</span><span>打印第一行后最后一行都打*连接($i==1 || $i==5) </span> <span>if</span>(<span>$i</span>==1){ <span>//</span><span>去掉$i==5 把中间抛空</span> <span>echo</span> "*"<span>; }</span><span>else</span><span>{ </span><span>//</span><span>怎么打空格和*号的问题</span> <span>if</span>(<span>$k</span>==1 || <span>$k</span>==2*<span>$i</span>-1<span>){ </span><span>echo</span> "*"<span>; }</span><span>else</span><span>{ </span><span>echo</span> " "<span>; } } } </span><span>echo</span> "<br/>"<span>; } </span><span>//</span><span>倒转 抛空</span> <span>for</span>(<span>$i</span>=<span>$n</span>;<span>$i</span>>=0;<span>$i</span>--<span>){ </span><span>//</span><span>打印空格</span> <span>for</span>(<span>$j</span>=0;<span>$j</span><=<span>$n</span>-<span>$i</span>;<span>$j</span>++<span>){ </span><span>echo</span> " "<span>; } </span><span>//</span><span>打印*号</span> <span>for</span>(<span>$k</span>=1;<span>$k</span><=2*<span>$i</span>-3;<span>$k</span>++<span>){ </span><span>//</span><span>怎么打空格和*号的问题</span> <span>if</span>(<span>$k</span>==2*<span>$i</span>-3 || <span>$k</span>==1<span>){ </span><span>echo</span> "*"<span>; }</span><span>else</span><span>{ </span><span>echo</span> " "<span>; } } </span><span>echo</span> "<br/>"<span>; } </span>?>
Rendering:
Finally done. By the way, programming is actually not difficult. The difficulty lies in the way of thinking and how to solve the problem. So everyone is good at thinking during the programming process. Although it took me half a day to think about it, I finally figured it out and the problem was solved, so I usually have to use my brain and practice more. Our programming skills can be improved. Only by practicing can things be understood and knowledge points mastered.