PHP Practice-Print a hollow diamond with a for loop, for Diamond _php tutorial

Source: Internet
Author: User

PHP Exercises--print a hollow diamond with a for loop, for Diamond


Idea: 1, write code to print a solid pyramid

2. Modify the code to empty the pyramid

3. Revise the code to reverse the pyramid

4. Revise the inverted code to empty the pyramid

5, modify the code, the specific number of the parameterization

1. Write code to print the Pyramid of Solid Gold:

 
  PHP    //$n =5;     for ($i= 1; $i<=5; $i+ +)        {// print space for        ($j=1;  $j<=5-$i; $j+ +)            {echo ""        ;        } // Print * Number         for ($k= 1; $k<=2*$i-1; $k+ +)            {echo "*"        ;        } Echo "
"; }? >

2.

2. Improve the code, throw the pyramid empty

 Php//$n =5;     for($i= 1;$i<=5;$i++){        //Print Space         for($j= 1;$j<=5-$i;$j++){            Echo" "; }    //Print * Number     for($k= 1;$k<=2*$i-1;$k++){        //after the first line is printed, the last line is called * Connected        if($i==1 | |$i==5){            Echo"*"; }Else{        //How to play the blanks and the * number problem            if($k==1 | |$k==2*$i-1){                Echo"*"; }Else{                Echo" "; }        }    }    Echo"
"; }?>

3. Revise the code to reverse the pyramid

 Php//$n =5;     for($i= 1;$i<=5;$i++){        //Print Space         for($j= 1;$j<=5-$i;$j++){            Echo" "; }        //Print * Number         for($k= 1;$k<=2*$i-1;$k++){            //after the first line is printed, the last line is called * Connected            if($i==1 | |$i==5){                    Echo"*"; }Else{                //How to play the blanks and the * number problem                if($k==1 | |$k==2*$i-1){                    Echo"*"; }Else{                    Echo" "; }            }        }    Echo"
"; } //Reversed for($i= 5;$i>=0;$i--){ //Print Space for($j= 0;$j<=5-$i;$j++){ Echo" "; } //Print * Number for($k= 1;$k<=2*$i-3;$k++){ Echo"*"; } Echo"
"; }?>

4, modify the inverted code, the pyramid is empty, and the middle of the * number to remove the code (if ($i ==1 | | $i ==5) {//Remove the middle of the $i==5 to throw empty)

 Php//$n =5;     for($i= 1;$i<=5;$i++){        //Print Space         for($j= 1;$j<=5-$i;$j++){            Echo" "; }        //Print * Number         for($k= 1;$k<=2*$i-1;$k++){        //The last line after the first line is printed * connection ($i ==1 | | $i ==5)            if($i==1) {//Remove the $i==5 and throw the middle .                Echo"*"; }Else{                //How to play the blanks and the * number problem                if($k==1 | |$k==2*$i-1){                    Echo"*"; }Else{                    Echo" "; }        }    }    Echo"
"; } //Reverse Empty for($i= 5;$i>=0;$i--){ //Print Space for($j= 0;$j<=5-$i;$j++){ Echo" "; } //Print * Number for($k= 1;$k<=2*$i-3;$k++){ //How to play the blanks and the * number problem if($k==2*$i-3 | |$k==1){ Echo"*"; }Else{ Echo" "; } } Echo"
"; }?>

5, modify the code, the specific number of parameterization: the specific number 5 to replace the $n=5, then you can set the value of $n, can be arbitrarily enlarged and reduced

 Php$n=5;  for($i= 1;$i<=$n;$i++){        //Print Space         for($j= 1;$j<=$n-$i;$j++){            Echo" "; }        //Print * Number         for($k= 1;$k<=2*$i-1;$k++){        //The last line after the first line is printed * connection ($i ==1 | | $i ==5)            if($i==1) {//Remove the $i==5 and throw the middle .                Echo"*"; }Else{                //How to play the blanks and the * number problem                if($k==1 | |$k==2*$i-1){                    Echo"*"; }Else{                    Echo" "; }        }    }    Echo"
"; } //Reverse Empty for($i=$n;$i>=0;$i--){ //Print Space for($j= 0;$j<=$n-$i;$j++){ Echo" "; } //Print * Number for($k= 1;$k<=2*$i-3;$k++){ //How to play the blanks and the * number problem if($k==2*$i-3 | |$k==1){ Echo"*"; }Else{ Echo" "; } } Echo"
"; }?>

Finally done. By the way, in fact, programming is not difficult, difficult in what kind of thinking, how to solve the problem of the method. So everyone is good at thinking in the process of programming. Although spent a half a day to think, but finally think out, the problem has been solved, so usually more brain to practice. We can improve our programming capabilities. Only practice, things can be understood, knowledge points also mastered.

http://www.bkjia.com/PHPjc/1128123.html www.bkjia.com true http://www.bkjia.com/PHPjc/1128123.html techarticle PHP Exercise--with a for loop to print the Hollow diamond, for diamond ideas: 1, write the code to print a solid pyramid 2, modify the code, throw the pyramid empty 3, modify the code, the pyramid reversed ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.