Home > Backend Development > PHP Tutorial > PHP exercise--use for loop to print hollow rhombus, for rhombus_PHP tutorial

PHP exercise--use for loop to print hollow rhombus, for rhombus_PHP tutorial

WBOY
Release: 2016-07-12 08:51:43
Original
1861 people have browsed it

PHP exercise--use for loop to print hollow diamonds, for diamonds

Ideas: 1. Write the code to print solid pyramids

2. Modify the code and short the pyramid

3. Modify the code and turn the pyramid upside down

4. Modify the inverted code and empty the pyramid

5. Modify the code and parameterize specific numbers

1. Write the code to print the pyramid with solid gold characters:

PHP exercise--use for loop to print hollow rhombus, for rhombus_PHP tutorial
<?<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>?>
Copy after login
PHP exercise--use for loop to print hollow rhombus, for rhombus_PHP tutorial

2,

2. Improve the code and empty the pyramid

PHP exercise--use for loop to print hollow rhombus, for rhombus_PHP tutorial
<?<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>?>
Copy after login
PHP exercise--use for loop to print hollow rhombus, for rhombus_PHP tutorial

3. Modify the code and turn the pyramid upside down

PHP exercise--use for loop to print hollow rhombus, for rhombus_PHP tutorial
<?<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>?>
Copy after login
PHP exercise--use for loop to print hollow rhombus, for rhombus_PHP tutorial

4. Modify the inverted code, empty the pyramid, and remove the * sign in the middle. Modify the code here (if($i==1 || $i==5){ //Remove $i= =5 and sell the middle short)

PHP exercise--use for loop to print hollow rhombus, for rhombus_PHP tutorial
<?<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>?>
Copy after login
PHP exercise--use for loop to print hollow rhombus, for rhombus_PHP tutorial

5. Modify the code and parameterize the specific number: change the specific number 5 to $n=5 instead, and then set the value of $n, which can be enlarged or reduced at will

PHP exercise--use for loop to print hollow rhombus, for rhombus_PHP tutorial
<?<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>?>
Copy after login
PHP exercise--use for loop to print hollow rhombus, for rhombus_PHP tutorial

Rendering:

Finally done. By the way, programming is actually not difficult. The difficulty lies in the way of thinking and how to solve the problem. So everyone is good at thinking during the programming process. Although it took me half a day to think about it, I finally figured it out and the problem was solved, so I usually have to use my brain and practice more. Our programming skills can be improved. Only by practicing can things be understood and knowledge points mastered.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1128123.htmlTechArticlePHP exercise--use for loop to print hollow diamonds, for diamond ideas: 1. Write the code to print the solid pyramid 2. Modify the code and empty the pyramid. 3. Modify the code and turn the pyramid upside down...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template