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

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

WBOY
Libérer: 2016-06-13 08:39:16
original
1836 Les gens l'ont consulté

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>?>
Copier après la connexion

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>?>
Copier après la connexion

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>?>
Copier après la connexion

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>?>
Copier après la connexion

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>?>
Copier après la connexion

 

效果图:

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

source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal