php while循环
2+22+222+2222+22...2怎么用while循环写出来?
释怀38
浏览 1619回答 1
1回答
-
习惯受伤
<?php
$x = 1;
$sum = 0;
$exp = "2";
while($x<3){
//存放拼接后的2
$ret = "";
$n = 1;
while($n++ <= $x){
$ret = $ret . "2";
}
//将拼接的2转化为整型
$num = intval($ret);
$sum += $num;
if($x>1){
$exp = $exp . " + " . $num;
}
$x += 1;
}
//输出总和
echo "Sum = " . $sum . "\r\n";
//输出表达式,不要可以删除
echo "Expression = " . $exp . " = " . $sum;
?>上一个效果图吧:
打开App,查看更多内容