简体   繁体   English

乘法游戏

[英]Multiplication Game

The Game Rules :-游戏规则:-

1- When the user first comes to the site a new expression is provided. 1- 当用户第一次访问网站时,会提供一个新的表达方式。

2- The user enters the guess and submits the form using the “Check” button. 2- 用户输入猜测并使用“检查”按钮提交表单。

3- The program checks the answer and if wrong provides a proper message and allows the user to keep guessing the same expression. 3- 程序检查答案,如果错误则提供正确的消息,并允许用户继续猜测相同的表达式。

4- If the user guesses the result, then a new expression is provided. 4- 如果用户猜测结果,则提供新的表达式。 The Game problem I face it is :-我面临的游戏问题是:-

1- Never get true result. 1-永远不会得到真正的结果。

2- The $expression not allows the user to keep guessing the same expression. 2- $expression 不允许用户继续猜测相同的表达式。

Note: I use random number between 1 and 12 inclusive function rand(1, 12)注意:我使用 1 到 12 之间的随机数,包括函数 rand(1, 12)

     <?php
        $a= rand(1, 12);

        $b= rand(1, 12);

        $message;

        $answer = null;

        $expression = $a ." X ".$b ;

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

             $message = "Welcome to the Multiplication progarm<br/>";

        }

        elseif ($_POST['guess'] == $a*$b) { // matches!

             $message = $a ." X ".$b." = ".$a*$b." is Correct!<br/> Now try the new expression";

             $answer = $_POST['guess'];

        }

        elseif ($_POST['guess'] != $a*$b) { // some other condition

             $message = $_POST['guess']." is Wrong!";

             $answer = $_POST['guess'];

        }

        ?>


        <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> 
 <?php
    session_start();//you must use session in the first 
    $_SESSION['expression'] = $_POST['expression'];//define session to make the expression in the same value. 
    $_SESSION['answer'] = $_POST['answer'];//define session to make the answer in the same value.
    elseif ($_POST['guess'] == $a*$b) { // matches!
         $message = $a ." X ".$b." = ".$a*$b." is Correct!<br/> Now try the new expression";
         session_destroy();//you must use this to stop session to produces new expression.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM