我这个计数的方式有问题吗?
<!--练习二 编写一个“猜数字游戏”的程序,在1到1000之间随机产生一个数--><!--然后请用户循环猜测这个数字,对于每个答案只回答“猜大了”或“猜小了”-->
<!--直到猜测准确为止,输出用户的猜测次数。-->
<?php
header("Content-type:text/html;charset=utf-8");
$n = mt_rand(0, 100);
$number = (isset($_POST['number'])) ? $_POST['number'] : $n;
$count = 0;
while (true){
$count = $count + 1;
if (!is_numeric(@$_POST['guess'])) {
$result = "输入非数字";
} elseif ($_POST['guess'] > $number) {
$result = "猜大了";
} elseif ($_POST['guess'] < $number) {
$result = "猜小了";
} else if ($_POST['guess'] == $number) {
$result = "猜对了" . "<br />" . "数字是:" . $_POST['guess']."<br />"."猜测次数为".$count;
}
}
?>
<p><?php echo "待猜测数字:" . $number ?></p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
输入你猜的数字:
<input type="text" id="guess" name="guess"/>
<input type="hidden" name="number" value="<?php echo $number ?>">
<input type="submit" name="button" value="判断">
<p><?php echo $result ?></p>
</form>