2+22+222+2222+22...2怎么用while循环写出来?
1 回答
习惯受伤
TA贡献885条经验 获得超1144个赞
<?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; ?>
上一个效果图吧:
添加回答
举报
0/150
提交
取消