首頁 >後端開發 >php教程 >PHP练习--用for循环打印空心菱形,for菱形_PHP教程

PHP练习--用for循环打印空心菱形,for菱形_PHP教程

WBOY
WBOY原創
2016-07-12 08:51:431847瀏覽

PHP练习--用for循环打印空心菱形,for菱形

思路: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>?>
复制代码

 

效果图:

最后大功告成。顺便说说 ,其实编程不难,难就难在怎么样的思路,怎样去解决问题的方法。所以大家在编程的过程中善于思考。尽管花了半天的时间去想,但是最后想出来了,问题也得到解决,所以平时要多动脑动手去实践。我们的编程能力才能提高。只有实践了,东西才能被理解,知识点也掌握了。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1128123.htmlTechArticlePHP练习--用for循环打印空心菱形,for菱形 思路:1,写出代码打印实心的金字塔 2、修改代码,把金字塔抛空 3、修改代码,把金字塔倒转...
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn