Home > Backend Development > PHP Tutorial > 非常简单的数学题,怎么写呢?

非常简单的数学题,怎么写呢?

WBOY
Release: 2016-06-23 14:18:11
Original
1127 people have browsed it

$i=0;
while ($i {
$i++;
if ($i%2==0)
{
continue;
}
echo "$i
";
}
?>
我想显示的是0-20之间的奇数,问题是总到21,怎么办?


回复讨论(解决方案)

不更改0

不更改while ($i

...
$i++;
if($i>20) break;
...

把$i++;移到echo "$i
";下面

$i=0;while ($i<=20) {  if ($i%2==1) {    echo "$i<br>";  }  $i++;}
Copy after login

解决方案上面都说了
我说说你的思路问题
一进入while,未经其他任何有用的处理,立即就$i++
说明你的循环实质是对1~21进行计算,而不是0~20,这就是问题所在

i++  移到后面

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template