dougu3591
2012-11-19 01:21
浏览 126
已采纳

PHP星号三角形对齐

I'm new to web programming and recently started getting into PHP. I wrote a little code to produce a "right angle triangle". I recognize that using &nbsp is part of the solution but I've placed it in every possible place with no luck so any advice would be appreciated..Below you will find the coding/current output/desired output:

$x = 10;
while ( $x >= 1 ) {
    $y=1;
    while ($y <= $x) {
        echo "*";
        ++$y;
    }
    echo "<br/>";
    --$x;
}

output:
**********
*********
********
*******
******
*****
****
***
**
*


desire output:
**********
 *********
  ********
   *******
    ******
     *****
      ****
       ***
        **
         *
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • duanji5746 2012-11-19 01:24
    已采纳

    You want 10 characters per line, n asterix and 10 - n spaces. Knowing this, you just need to add another loop inside to control how many spaces to output!

    Something as simple as:

    $x = 10;
    while ( $x >= 1 ) {
        $spaces = 1;
        while($spaces <= 10 - $x)
        {
            echo "&nbsp";
            ++$spaces;
        }
        $y=1;
        while ($y <= $x) {
            echo "*";
            ++$y;
        }
        echo "<br/>";
        --$x;
    }
    
    点赞 打赏 评论
  • du131642 2012-11-19 02:35

    Here's my suggestion; without whiles, but using a for loop (and str_repeat()s).

    echo '<pre>'; // just here for display formatting
    
    // $x = current number of *
    // $t = total number of positions
    for( $x = $t = 10; $x > 0; $x-- )
    {
        // repeat '&nbsp;' $t - $x times
        // repeat '*' $x times
        // append '<br>'
        echo str_repeat( '&nbsp;', $t - $x ) . str_repeat( '*', $x ) . '<br>';
    }
    

    And with a while loop:

    echo '<pre>'; // just here for display formatting
    
    $x = $t = 10;
    while( $x > 0 )
    {
        echo str_repeat( '&nbsp;', $t - $x ) . str_repeat( '*', $x ) . '<br>';
        --$x;
    }
    

    To echo your original triangle, just switch the str_repeat()s.

    点赞 打赏 评论
  • dps57553 2012-11-19 02:57
    <?php
    
    $num = 10;
    $char = '*';
    $string = str_repeat($char, $num);
    
    for ($i = 1; $i <= $num; $i++)
    {
        printf("%{$num}s
    ", $string);
        $string = substr_replace($string, '', -1);
    }
    
    ?>
    

    Wrap in <pre> tags if you want easy formatting.

    点赞 打赏 评论
  • dongpaocuan7498 2016-06-09 19:19

    I hope this code may be helpfull for you

    new code to create star triangle using call_user_func() and for loop()

    Star triangle

    *
    **
    ***
    ****
    *****
    

    Php code

    <?php
    /**
      | Code for display star triangle using for loop 
      | date : 2016-june-10
      | @uther : Aman kumar
      */
    
    $sum = "*";
    for($i=0;$i<=5;$i++)
    {
        call_user_func(function($sum) { echo $sum, "<br/>"; }, str_repeat($sum,$i));
    }
    ?>
    
    点赞 打赏 评论

相关推荐 更多相似问题