Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的HTML和PHP代码没有提供预期的输出_Php_Html - Fatal编程技术网

我的HTML和PHP代码没有提供预期的输出

我的HTML和PHP代码没有提供预期的输出,php,html,Php,Html,我一直在尝试为猜谜游戏编写代码。用户必须猜测1-99之间的随机奇数。这是我的密码- <?php $heading = "Welcome to the guessing game"; $num_to_guess = rand(1,99); if(!isset($_POST['guess'])){ $message= "Welcome to the guessing game"; } elseif($_POST['guess'] %2==0) { $message= "Th

我一直在尝试为猜谜游戏编写代码。用户必须猜测1-99之间的随机奇数。这是我的密码-

<?php

$heading = "Welcome to the guessing game";
$num_to_guess = rand(1,99);
if(!isset($_POST['guess'])){
    $message= "Welcome to the guessing game";

} elseif($_POST['guess'] %2==0) {
    $message= "The number you are guessing is not odd!!!";

} elseif($_POST['guess']< $num_to_guess){
    $message= "Your guess is smaller than the secret number";

}elseif($_POST['guess']> $num_to_guess){
    $message= "Your guess is bigger than the secret number";

} elseif($_POST['guess']== $num_to_guess){
    $message= "You guessed correctly.Now try to guess another number!";
}

?>
<!DOCTYPE html>
<html>
<head>
<title> Guessing Machine </title>
</head>
<h1> <?php echo $message; ?></h1>
<body>
<form action="" method="POST">
<p><label for= "guess">Try to guess the odd number between 1 and 99 </label>
<input type= "text" is= "guess" name= "guess" /></p>
<br>
<input type="submit" name="check" value="Check"/><br><br>
</form>
</body>
</html>

猜谜机
试着猜1到99之间的奇数




现在我得到了前四条if语句所需的输出。我该怎么做才能得到这样的信息:“你猜对了!现在试着猜另一个数字!”?
我很困惑。如果有人能帮忙的话,那真的很有帮助。谢谢。

在php中,任何变量在页面呈现后都会被销毁,因为您无法使用常用变量保存
$num\u to\u guess
数字。您需要设置会话并使用多个会话

我将您的代码更改为:

<?php

$heading = "Welcome to the guessing game";
if (!isset($_SESSION['g_number'])){
    $_SESSION['g_number'] =  rand(1,99);
}
$num_to_guess = $_SESSION['g_number'];
if(!isset($_POST['guess'])){
    $message= "Welcome to the guessing game";

} elseif($_POST['guess'] %2==0) {
    $message= "The number you are guessing is not odd!!!";

} elseif($_POST['guess']< $num_to_guess){
    $message= "Your guess is smaller than the secret number";

}elseif($_POST['guess']> $num_to_guess){
    $message= "Your guess is bigger than the secret number";

} elseif($_POST['guess']== $num_to_guess){
    $message= "You guessed correctly.Now try to guess another number!";
    unset($_SESSION['g_number']);
}

?>


你必须键入
$num\u来猜测
随机数?你获得所需输出的几率是1/200。请记住,每个表单提交都会产生一个新的随机数。(其他:会话)@Fast Snail,是的,一个随机数。但是我如何得到最后一条消息呢?代码中似乎没有任何东西限制
num\u to\u guess
变量为奇数。你一点也不试双数吗?尝试打印变量以查看其实际值,然后输入该值以查看其是否有效。如果希望更容易猜测数字,请更改
rand(1,99)至<代码>兰德(1,5)-你更可能猜到正确的数字,然后你可以检查你的消息是否出来。