我正在尝试编写一个简单的do / while语句,它创建一个随机数,然后检查该数字是否低于某个阈值,如果是,则应该停止while循环。然而它似乎并不适合我,我无法弄清楚为什么。
我猜它会变得非常简单,但我现在还不知道。
这是我的代码,非常感谢帮助!
<!DOCTYPE html>
<html>
<head>
<title>Your own do-while</title>
<link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
<?php
$rayBans = rand(0,70);
$correctPrice = false;
do {
echo "<p> Lets hope I can get ray bans for under £30! </p>";
}
while ($correctPrice == false);
if ($rayBans > 30) {
echo "<p> raybans at $rayBans are too expensive </p>";
$correctPrice == false;
}
else if ($rayBans < 30){
echo "<p> Finaly got my rayBans for $rayBans </p>";
$correctPrice == true;
}
?>
</body>
</html>
答案 0 :(得分:1)
在比较时我们应该使用==
,同时将值分配给变量,我们需要=
,所以在您的情况下
do
{
//You should write something so that $correctPrice becomes true
//as of now it seems to be a infinite loop
echo "<p> Lets hope I can get ray bans for under £30! </p>";
}while ($correctPrice == false);
我相信你需要这样做
<?php
$correctPrice = false;
do
{
$rayBans = rand(0,70);
echo "<p> Lets hope I can get ray bans for under £30! </p>";
if ($rayBans > 30)
{
echo "<p> raybans at $rayBans are too expensive </p>";
$correctPrice = false;
}
else if ($rayBans < 30)
{
echo "<p> Finaly got my rayBans for $rayBans </p>";
$correctPrice = true;
}
}while ($correctPrice == false);
?>
答案 1 :(得分:-2)
更正您的代码,为您提供do-while循环条件
echo "<p> Lets hope I can get ray bans for under £30! </p>";
$correctPrice = false;
do {
$rayBans = rand(0,70);
if ($rayBans > 30) {
echo "<p> raybans at $rayBans are too expensive </p>";
$correctPrice = false;
}
else if ($rayBans < 30){
echo "<p> Finaly got my rayBans for $rayBans </p>";
$correctPrice = true;
}
}
while ($correctPrice == false);