通过游戏循环保留两个随机数

时间:2016-05-03 13:57:33

标签: php random

我的代码应该提供两个随机数,让用户输入他们的产品(乘法)。

如果输入错误的答案,它会告诉您再次猜测,但保留相同的随机数,直到您正确回答。正确回答,并从一对新数字开始。

即使输入了错误的数字,下面的代码也会更改两个随机数的值。我想保持值相同,直到输入正确的答案。

<?php
$num1=rand(1, 9);
$num2=rand(1, 9);
$num3=$num1*$num2;
$num_to_guess = $num3;
echo $num1."x".$num2."= <br>";


if ($_POST['guess'] == $num_to_guess) 
        { // matches!
            $message = "Well done!";
        } 
        elseif ($_POST['guess'] > $num_to_guess) 
        {
            $message = $_POST['guess']." is too big! Try a smaller number.";
        } 
            elseif ($_POST['guess'] < $num_to_guess) 
            {
                $message = $_POST['guess']." is too small! Try a larger number.";
            } 
                else  
                { // some other condition
                    $message = "I am terribly confused.";
                }
?>
<!DOCTYPE html>
<html>
<body>
<h2><?php echo $message; ?></h2>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="answer" value="<?php echo $answer;?>">
<input type="hidden" name="expression" value="<?php echo $expression;?>">
What is the value of the following multiplication expression: <br><br>
<?php echo $expression; ?> <input type="text" name="guess"><br>
<input type="submit" value="Check">
</form>
</body>
</html>

3 个答案:

答案 0 :(得分:0)

为了保持相同的数字,你必须将它们存储在页面上,然后在使用php提交表单时检查它们。如果表单从未提交,您还必须设置随机数。在您的情况下,您总是更改num1和num2。我试图尽可能多地保留原始代码,但仍需要一些工作来简化它。

  1. 首先,我在名为num1和num2
  2. 的html中添加了2个隐藏字段
  3. 其次,我将$ num1和$ num2设置为从表单提交的值。
  4. 在完成其余逻辑之后,如果答案是正确的,我确保$ num1和$ num2被重置。表单从未提交过。
  5. 您可以在下面的代码中看到评论。

    此外,如果您打算在生产环境中使用此功能,则需要验证从表单传入的值,以便恶意用户不会利用您的代码。 :)

     <?php
    // Setting $num1 and $num2 to what was posted previously and performing the math on it.
    $num1 = $_POST['num1'];
    $num2 = $_POST['num2'];
    $num_to_guess = $num1*$num2;
    
    // Check for the correct answer
    if ($_POST && $_POST['guess'] == $num_to_guess)
    {
        // matches!
        $message = "Well done!";
        $num1=rand(1, 9);
        $num2=rand(1, 9);
    } 
    // Give the user a hint that the number is too big
    elseif ($_POST['guess'] > $num_to_guess)
    {
        $message = $_POST['guess']." is too big! Try a smaller number.";
    } 
    // Give the user a hint that the number is too small
    elseif ($_POST['guess'] < $num_to_guess) 
    {
        $message = $_POST['guess']." is too small! Try a larger number.";
    } 
    // If the form wasn't submitted i.e. no POST or something else went wrong
    else  
    {
        // Only display this message if the form was submitted, but there were no expected values
        if ($_POST)
        {
            // some other condition and only if something was posted
            $message = "I am terribly confused.";
        }
        // set num1 and num2 if there wasn't anything posted
        $num1=rand(1, 9);
        $num2=rand(1, 9);
    }
    
    // Show the problem
    echo $num1."x".$num2."= <br>";
    
    ?>
    <!DOCTYPE html>
    <html>
    <body>
    <h2><?php echo $message; ?></h2>
    <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="hidden" name="num1" value="<?= $num1 ?>" />
    <input type="hidden" name="num2" value="<?= $num2 ?>" />
    <input type="hidden" name="answer" value="<?php echo $num3;?>">
    <input type="hidden" name="expression" value="<?php echo $expression;?>">
    What is the value of the following multiplication expression: <br><br>
    <input type="text" name="guess"><br>
    <input type="submit" value="Check">
    </form>
    </body>
    </html>
    

答案 1 :(得分:0)

当您第一次加载页面时,您有(即)“2 * 3”作为问题。 $_POST未定义,因此if ($_POST['guess']...将生成未定义索引警告。然后你回复$message,但你定义$message的位置? $_POST['guess']未定义,因此评估为0,$num_to_guess为6(= 2 * 3),因此$message设置为“太小!尝试更大的数字。”,甚至如果用户没有输入任何内容。隐藏的answer设置为$answer,但未定义此变量,因此将其设置为空(或注意“通知:未定义的变量:回答”,如果激活错误报告)。 expression输入和echo $expression的相同问题。

尝试这样的事情:

$newQuestion = True;    // This variable to check if a new multiplication is required
$message     = '';

/* $_POST['guess'] check only if form is submitted: */
if( isset( $_POST['guess'] ) )
{
    /* Comparison with answer, not with new result: */
    if( $_POST['guess'] == $_POST['answer'] )
    {
        $message = "Well done!";
    }
    else
    {
        /* If result if wrong, no new question needed, so we propose same question: */
        $newQuestion = False;
        $answer      = $_POST['answer'];
        $expression  = $_POST['expression'];
        if( $_POST['guess'] > $_POST['answer'] )
        {
            $message = "{$_POST['guess']} is too big! Try a smaller number.";
        }
        else
        {
            $message = "{$_POST['guess']} is too small! Try a larger number.";
        }
    }
}

/* New question is generated only on first page load or if previous answer is ok: */
if( $newQuestion )
{
    $num1       = rand( 1, 9 );
    $num2       = rand( 1, 9 );
    $answer     = $num1*$num2;
    $expression = "$num1 x $num2";
    if( $message ) $message .= "<br>Try a new one:";
    else           $message  = "Try:";
}

?>

<!DOCTYPE html>
(... Your HTML Here ...)

答案 2 :(得分:0)

这也可能很有趣。这是一个会话。让您暂时存储一些东西。这有点脏。但要学习有趣。 http://www.w3schools.com/php/php_sessions.asp

 <?php
    session_start(); // Starts the Session.

    function Save() { // Function to save $num1 and $num2 in a Session.
        $_SESSION['num1'] = rand(1, 9);
        $_SESSION['num2'] = rand(1, 9);
        $_SESSION['num_to_guess'] = $_SESSION['num1']*$_SESSION['num2'];;
        $Som = 'Guess the number: ' . $_SESSION['num1'] .'*' .$_SESSION['num2'];
    }
     // If there is no session set
     if (!isset($_SESSION['num_to_guess'])) {
         Save();
         $message = "";
     }

     if (isset($_POST['guess'])) {
         // Check for the correct answer
         if ($_POST['guess'] == $_SESSION['num_to_guess']) {
             $message = "Well done!";
             session_destroy(); // Destroys the Session.
             Save(); // Set new Sessions.
         }
         // Give the user a hint that the number is too big
         elseif ($_POST['guess'] > $_SESSION['num_to_guess']) {
             $message = $_POST['guess']." is too big! Try a smaller number.";
             $Som = 'Guess the number: ' . $_SESSION['num1'] .'*' .$_SESSION['num2'];
         }
         // Give the user a hint that the number is too small
         elseif ($_POST['guess'] < $_SESSION['num_to_guess']) {
             $message = $_POST['guess']." is too small! Try a larger number.";
             $Som = 'Guess the number: ' . $_SESSION['num1'] .'*' .$_SESSION['num2'];
         }
         // some other condition
         else { 
             $message = "I am terribly confused.";
         }
    }
    ?>

    <html>
    <body>
    <h2><?php echo $Som . '<br>'; ?>
        <?php echo $message; ?></h2>
        <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            <input type="text" name="guess"><br>
            <input type="submit" value="Check">
        </form>
    </body>
    </html>