思路:1,写出代码打印实心的金字塔
2、修改代码,把金字塔抛空
3、修改代码,把金字塔倒转
4、修改倒转后的代码,把金字塔抛空
5、修改代码,把具体的数进行参数化
1、写出代码打印实心金字的金字塔:
<?<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、改善代码,把金字塔抛空
<?<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、修改代码,把金字塔倒转
<?<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、修改倒转后的代码,把金字塔抛空,并把中间的*号去掉 修改这里的代码(if($i==1 || $i==5){ //去掉$i==5 把中间抛空)
<?<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、修改代码,把具体的数进行参数化 :把具体数字5改成$n=5代替,随后可以设置$n的值,可以任意放大和缩小
<?<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>?>
效果图:
最后大功告成。顺便说说 ,其实编程不难,难就难在怎么样的思路,怎样去解决问题的方法。所以大家在编程的过程中善于思考。尽管花了半天的时间去想,但是最后想出来了,问题也得到解决,所以平时要多动脑动手去实践。我们的编程能力才能提高。只有实践了,东西才能被理解,知识点也掌握了。