簡體   English   中英

乘法游戲

[英]Multiplication Game

游戲規則:-

1- 當用戶第一次訪問網站時,會提供一個新的表達方式。

2- 用戶輸入猜測並使用“檢查”按鈕提交表單。

3- 程序檢查答案,如果錯誤則提供正確的消息,並允許用戶繼續猜測相同的表達式。

4- 如果用戶猜測結果,則提供新的表達式。 我面臨的游戲問題是:-

1-永遠不會得到真正的結果。

2- $expression 不允許用戶繼續猜測相同的表達式。

注意:我使用 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