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

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

WBOY
풀어 주다: 2016-06-13 08:39:16
원래의
1837명이 탐색했습니다.

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>?>
로그인 후 복사

 

效果图:

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

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿