首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过博弈循环保持两个随机数

通过博弈循环保持两个随机数
EN

Stack Overflow用户
提问于 2016-05-03 13:57:32
回答 3查看 93关注 0票数 0

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

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

下面的代码更改了两个随机数的值,即使我输入了错误的数字。在输入正确答案之前,我希望将值保持不变。

代码语言:javascript
复制
<?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>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-05-03 14:34:39

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

  1. 首先,我在html中添加了两个名为num1和num2的隐藏字段。
  2. 其次,我将$num1和$num2设置为表单中提交的值。
  3. 在遵循其余的逻辑之后,我确保$num1和$num2被重置--如果答案是正确的--表单从未提交过。

您可以在下面的代码中看到注释。

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

代码语言:javascript
复制
 <?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>
票数 0
EN

Stack Overflow用户

发布于 2016-05-03 14:40:45

当您第一次加载页面时,您有(即)“2*3”作为问题。$_POST未定义,因此if ($_POST['guess']...将生成未定义的索引警告。然后回显$message,但是在哪里定义$message$_POST['guess']是未定义的,因此计算为0,$num_to_guess为6 (=2*3),因此$message设置为“太小了!尝试更大的数字。”,即使用户没有输入任何内容。隐藏的answer被设置为$answer,但是这个变量没有被定义,所以它被设置为空(或者“注意:未定义的变量:答案”,如果您激活了错误报告)。对于expression输入和echo $expression,同样的问题。

试着做这样的事情:

代码语言:javascript
复制
$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 ...)
票数 0
EN

Stack Overflow用户

发布于 2016-05-03 14:53:34

这也可能是有趣的学习。这是一次会议。让你暂时储存一些东西。有点脏。但也很有趣。sessions.asp

代码语言:javascript
复制
 <?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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37006300

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档