-11

我是 PHP 新手,正在尝试编写一个程序来生成一系列加法和(涉及两个随机生成从 0 到 100 的两个数字)。然后它需要让用户知道他们是否成功并询问他们是否要继续。我已经通过该网站进行了搜索,但似乎没有类似的答案。提前谢谢你1

这就是我到目前为止所做的;

  <html>
<head><title>Random addition</title></head>
<body>
<p>
<?php

$first_number = rand(1,100);
$second_number = rand(1,100);
$direct_text = "What the sum of these two number; " . "  ";
echo ($direct_text . $first_number . "". "+". "" .  $second_number) . "=". "<br />";

echo ("What do you think the answer is ?". "<br />");
?>

<?php
//If form not submitted, display form.
if (!isset($_POST['submit'])){
?>
<form method="post" action="addition.php">
  <p>Answer:
    <input type="text" name="answer">
  </p>
  <p>
    <input type="submit" name="submit" value="Submit">
  </p>
</form>

<?php
//If form submitted, process input.
}else{
//Retrieve string from form submission.
$answer = $_POST['answer'];
}
?>

<?php

$sum_total = $first_number + $second_number;


//$direct_text = "The two variables added together =  " . "<br />";
//print ($direct_text . $sum_total);

if ($answer == $sum_total) {
    echo ("Well done, Your right!" . "The answer is " . $sum_total .  "<br / >");
}
else
{
    echo ("Bad Luck.  " . " The answer is  " . $sum_total . "<br / >") ;
}
 echo "Do you want to try again?" . "<br / >" .  "y or n (y for yes and n for no)"


?>

</p>

</body>
</html>

我已经编写了一个 if else 语句来使用 $answer 变量,但它带来了我没有定义变量但我认为我有的错误。

4

1 回答 1

2

你好,你也曾经的朋友

<?php 
$a=rand(0,100); 
$b=rand(0,100); 
$c=$a+$b;
echo $a."+".$b."=".$c;  
?>
于 2013-08-06T04:24:20.943 回答