简体   繁体   中英

what's wrong with my while-loop php

Create a while-loop that subtracts 6.82 from the number 761 until the number is between (not equal to) 23 and 33. Answer with the final result asa float, rounded to 2 decimals.

$f=761;
$b=6.82;
while (($f > 33)||($f < 23 )) {
$f=$f-$b;
$f++;

}
$ANSWER = round($f, PHP_ROUND_HALF_DOWN);

I get the wrong answer, 27.68 instead of 31.26 on the book

我认为您不需要$f++

I have checked your loop, and before last $f equals 33.5 because of $f++; so it will loop once more. It is like:

f = 50.96
f = 45.14
f = 39.32
f = 33.5 // still higher than 33 so do it once more
f = 27.68 // and now is lower, get out

It seems like $f++ is not necessary here.

You can check both codes:

PHP Compiler

You dont need this:

 $f++

Remove it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM