Heim > php教程 > php手册 > PHP练习--用for循环打印空心菱形,for菱形

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

WBOY
Freigeben: 2016-06-13 08:39:16
Original
1837 Leute haben es durchsucht

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>?>
Nach dem Login kopieren

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>?>
Nach dem Login kopieren

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>?>
Nach dem Login kopieren

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>?>
Nach dem Login kopieren

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>?>
Nach dem Login kopieren

 

效果图:

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

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage