Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
循环金字塔形状的PHP嵌套_Php_For Loop - Fatal编程技术网

循环金字塔形状的PHP嵌套

循环金字塔形状的PHP嵌套,php,for-loop,Php,For Loop,使用PHP中的嵌套for循环,我必须创建以下模式: - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - + + + - - - - - - - - - - - + + + + + - - - - - - - - - + + + + + + + - - - - - - - + + + + + + + + + - - - - - + + + + + + + + + + + - - - + + + + + +

使用PHP中的嵌套for循环,我必须创建以下模式:

- - - - - - - - - - - - - - -
- - - - - - - + - - - - - - -
- - - - - - + + + - - - - - -
- - - - - + + + + + - - - - -
- - - - + + + + + + + - - - -
- - - + + + + + + + + + - - -
- - + + + + + + + + + + + - -
- + + + + + + + + + + + + + -
+ + + + + + + + + + + + + + +
我尝试过这样做,并编写了以下代码:

$pluscount = -1;
$mincount  = 8;
for ($rows = 0; $rows <= 8; $rows++) {
    for ($min = 0; $min < $mincount; $min++) {
        echo " - ";
    }
    for ($plus = 0; $plus < $pluscount; $plus++) {
        echo " + ";
    }
    for ($min = 0; $min < $mincount; $min++) {
        echo " - ";
    }
    $pluscount += 2;
    $mincount  = (15 - $pluscount) / 2;
    echo "<br />";
}

如您所见,第一行不正确。我该如何解决这个问题?

这是一种处理问题的老套方法。总的来说,我不喜欢这个解决方案

$pluscount = -1;
$mincount  = 7;
for ($rows = 0; $rows <= 8; $rows++) {
    for ($min = 0; $min < $mincount; $min++) {
        echo " - ";
    }

    for ($plus = 0; $plus < $pluscount; $plus++) {
        echo " + ";
    }

    if ($rows == 0) {
            $mincount += 1;
    }

    for ($min = 0; $min < $mincount; $min++) {
        echo " - ";
    }
    $pluscount += 2;
    $mincount  = (15 - $pluscount) / 2;
    echo "<br />";
}
$pluscont=-1;
$mincount=7;
对于($rows=0;$rows 0){
//字符串替换开始位置=开始索引-plusCount;编号=plusCount
}否则{
//基线
}
}

您可以使用替换子回路,并使用修剪最后一个零件

for ( $row = 8; $row >= 0; $row-- ) {
    echo substr( 
       str_repeat( " - ", $row ) . str_repeat( " + ", max( 15 - ($row*2), 0 ) ) . str_repeat( " - ", $row ), 
    0, 45 );
}
$pluscont=-1;
$mincount=8;

对于($rows=0;$rows我知道已经选择了最好的答案,但这让我回到了一切都简单得多的时候,我刚刚开始学习编程..忍不住走过记忆的小路;)

$xLength=15;
$yLength=9;
$fillerChar='-';
$outputChar='+';
$drawPoint=地板($X长度/2);
$endPoint=$drawPoint;
对于($yPosition=0;$yPosition


之所以会发生这种情况,是因为你有两个for循环,从0到8=16个字符。解决了这个问题,你的问题就解决了。我知道,我只是不知道怎么做。与其让你的第三个循环成为绝对的0->mincount,不如让它成为$left\u over\u spaces->$maxwidth,这其实很聪明……编程就是为了实现它有创造力和发明新的做事方式,不是吗?:)谢谢你的回答!哇,看起来很复杂!不幸的是,我们不得不使用嵌套for循环(for循环中的for循环)。另外,这个作业还有几周就要交了,我只是想试试:)。我想我们会在上周左右得到解释。不过还是谢谢你的回答!没问题,对我来说,它看起来更简单,但如果它是一个家庭作业,我理解循环的用法。哇,那就更好了!非常感谢你!
//Pseudo-code
plusCount = -1;
baseString = "---------------";
startIndex = 7;
for (row = 0; row < 10; row++) {
     if(plusCount > 0) {
         //string replace startingLocation = startIndex - plusCount; number = pluscount
     } else {
         //baseString
     }
}
for ( $row = 8; $row >= 0; $row-- ) {
    echo substr( 
       str_repeat( " - ", $row ) . str_repeat( " + ", max( 15 - ($row*2), 0 ) ) . str_repeat( " - ", $row ), 
    0, 45 );
}
$pluscount = -1;
$mincount  = 8;
for ($rows = 0; $rows <= 8; $rows++) {
    for ($min = 0; $min < $mincount; $min++) {
        echo " - ";
    }
    for ($plus = 0; $plus < $pluscount; $plus++) {
        echo " + ";
    }
    for ($min = 0; $min < min($mincount, 7); $min++) {
        echo " - ";
    }
    $pluscount += 2;
    $mincount  = (15 - $pluscount) / 2;
    echo "<br />";
}
$xLength = 15;
$yLength = 9;
$fillerChar = '-';
$outputChar = '+';

$drawPoint = floor($xLength/2);
$endPoint  = $drawPoint;

for ($yPosition=0; $yPosition<$yLength; $yPosition++) {

    for ($xPosition=0; $xPosition<$xLength; $xPosition++) {

        if (($drawPoint < $xPosition) && ($xPosition < $endPoint)) {

            print $outputChar;

        } else {

            print $fillerChar;

        }
    }

    $drawPoint--;
    $endPoint++;

    print("\n");

}
<?php
    create_pyramid("+", 10);

    function create_pyramid($string, $level) {
        echo "<pre>";
        $level = $level * 2;
        print str_repeat("-",$level - 1)."<br/>";
        for($i = 1; $i <= $level; $i ++) {
            if (!($i % 2) && $i != 1)
                continue;
            print str_pad(str_repeat($string, $i),($level - 1) * strlen($string), "-" , STR_PAD_BOTH);
            print PHP_EOL;
        }
    }
?>