2016-04-30 42 views
-3

游戏规则: -乘法游戏

1-当用户第一次访问该网站时,提供了一个新的表达式。

2-用户输入猜测并使用“检查”按钮提交表单。

3-程序检查答案,如果错误提供了正确的信息,并允许用户继续猜测相同的表达式。

4-如果用户猜测结果,则提供新的表达式。 我面对的游戏问题是: -

1-永远不会得到真实的结果。

2- $表达式不允许用户继续猜测相同的表达式。

注: 我用随机数1和12包容性功能兰特之间(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> 
+1

在每个页面加载重置的'$了'值/'$ B' ,然后检查'elseif($ _POST ['guess'] == $ a * $ b)'。你或者需要检查'elseif($ _POST ['guess'] == $ _POST ['answer'])'** OR **将结果保存在'$ _SESSION'中。 #2的类似问题,因为您需要使用'$ _POST ['expression']'或再保存到'$ _SESSION'。 – Sean

+0

请参阅http://php.net/manual/en/function.session-start.php,并尝试像@Sean已经解释的那样自行学习基础知识。 –

回答

0
<?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. 
+0

这仍然有OP的问题,因为你仍然有'elseif($ _POST ['guess'] == $ a * $ b)',所以它与新的'$ a'&'$ b'而不是'$ _POST ['answer']'或$ _SESSION ['answer']' – Sean