Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.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
比较数学游戏PHP中的整数_Php_Math_Post_Int_Operators - Fatal编程技术网

比较数学游戏PHP中的整数

比较数学游戏PHP中的整数,php,math,post,int,operators,Php,Math,Post,Int,Operators,我做了一个简单的数学游戏,用户可以回答问题,然后检查输入的答案是否正确 我想让它在回答正确时说正确,在回答不正确时说不正确。到目前为止,我还没有看到正确的字符串出现,即使我知道我输入的答案是正确的 我相信这与我的$\u帖子有关,但我不能完全确定。我怎样才能解决这个问题 <html> <head><title>Addition php</title></head> <body> <h1>MATH GAME WOOO

我做了一个简单的数学游戏,用户可以回答问题,然后检查输入的答案是否正确

我想让它在回答正确时说正确,在回答不正确时说不正确。到目前为止,我还没有看到正确的字符串出现,即使我知道我输入的答案是正确的

我相信这与我的
$\u帖子有关,但我不能完全确定。我怎样才能解决这个问题

<html>
<head><title>Addition php</title></head>
<body>
<h1>MATH GAME WOOOO</h1>

    <form action="<?php $PHP_SELF?>" method="POST">
            <input type="text" name="guess"><br>
            <input type="submit" name="guess-butt"><br>
    </form>

<?php

    $operators = array("+", "-", "*", "/");
    $operator = $operators[rand(0,2)];
    $rand_int1 = rand(0, 10);
    $rand_int2 = rand(0, 10);

    echo("<a>What is " . $rand_int1 . ' ' . $operator . ' ' . $rand_int2 . "?</a><br>");
    echo('guess for last question: ' . $_POST['guess'] . '<br>');

if (isset($_POST['guess'])) {

    $guess = intval($_POST['guess']);

    if ($operator == "+") 
    {
        $temp = $rand_int1 + $rand_int2;
        echo('answer: ' . $temp . "<br>" . '');
        if ($guess == $temp) 
        {
            echo("<br>correct<br>");
        }
        else
        {
            echo('<br>incorrect<br>');
        }
    } 

    elseif ($operator == "-") 
    {
        $temp = $rand_int1 - $rand_int2;
        echo('answer: ' . $temp . "<br>" . '');
        if ($guess == $temp) 
        {
            echo("<br>correct<br>");
        }
        else
        {
            echo('<br>incorrect<br>');
        }
    } 

    elseif ($operator == "*") 
    {
        $temp = $rand_int1 * $rand_int2;
        echo('answer: ' . $temp . "<br>" . '');
        if ($guess == $temp) 
        {
            echo("<br>correct<br>");
        }
        else
        {
            echo('<br>incorrect<br>');
        }
    } 

    elseif ($operator == "/") 
    {
        $temp = $rand_int1 / $rand_int2;
        echo('answer: ' . $temp . "<br>" . '');
        if ($guess == $temp) {
            echo("<br>correct<br>");
        }
        else
        {
            echo('<br>incorrect<br>');
        }
    }
}

?>

</body>
</html>

加法php
数学游戏

每次重新加载页面时,变量
$rand_int1
$rand_int2
都会随机化。因此,如果用户看到消息“What is 5+6?”并回答它,页面将向自己提交一个POST请求,
$rand_int1
和2包含新的随机数字。所以很难得到正确的答案

您还可以尝试提交
$rand_int1
和2个变量作为隐藏输入字段:

  <input type="hidden" name="rand_int1" value="<?php echo $rand_int1; ?>">
  <input type="hidden" name="rand_int2" value="<?php echo $rand_int2; ?>">

$operators[rand(0,2)]永远不会选择
/
。您应该使用
array\u rand()
从数组中选择一个随机元素。我可以看到所有运算符…您的猜测来自上一个页面加载,该页面使用不同的随机值。您正在根据当前问题的计算测试上一个问题的答案。
$answer = $_POST['rand_int1'] + $_POST['rand_int2'];

echo('answer: ' . $answer . "<br>" . '');
if ($guess == $answer) 
{
    echo("<br>correct<br>");
}
else
{
    echo('<br>incorrect<br>');
}