在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 />";
}
然而,这导致:
- - - - - - - - - - - - - - - -
- - - - - - - + - - - - - - -
- - - - - - + + + - - - - - -
- - - - - + + + + + - - - - -
- - - - + + + + + + + - - - -
- - - + + + + + + + + + - - -
- - + + + + + + + + + + + - -
- + + + + + + + + + + + + + -
+ + + + + + + + + + + + + + +
如您所见,第一行不正确。我该如何解决这个问题?
答案 0 :(得分:2)
您可以使用str_repeat()替换子循环,并使用substr()修剪最后一部分。
var The_Function_I_Need = {
"x^2": function(x) {
return Math.pow(x, 2);
},
"x*2+5": function(x) {
return 2 * x + 5;
},
"x+y": function(x, y) {
return x + y;
}
}
console.log(The_Function_I_Need["x^2"](4))
console.log(The_Function_I_Need["x*2+5"](4))
console.log(The_Function_I_Need["x+y"](2, 4))
答案 1 :(得分:1)
这是解决问题的一种黑客方式。我不是整体解决方案的粉丝。
$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 />";
}
我认为你最好从一个15' - '字符串开始,并根据你所在的行替换你需要的数字。
//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
}
}
答案 2 :(得分:1)
$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 />";
}
答案 3 :(得分:0)
我知道最好的答案已经被选中,但它让我回到原点,当时一切都变得简单了,我开始学习编程......无法抵挡走过记忆的道路;)
$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");
}
答案 4 :(得分:0)
<?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;
}
}
?>