Php 关于在if-else语句中使用用户输入的一些帮助
我是PHP新手,正在尝试编写一个程序,该程序将生成一系列加法和(涉及两个随机数,从0到100生成两个数字)。然后需要让用户知道他们是否成功,并询问他们是否想继续。我在网站上搜索过,但似乎没有类似的答案。提前谢谢你 这就是我到目前为止所做的Php 关于在if-else语句中使用用户输入的一些帮助,php,random,sum,Php,Random,Sum,我是PHP新手,正在尝试编写一个程序,该程序将生成一系列加法和(涉及两个随机数,从0到100生成两个数字)。然后需要让用户知道他们是否成功,并询问他们是否想继续。我在网站上搜索过,但似乎没有类似的答案。提前谢谢你 这就是我到目前为止所做的 <html> <head><title>Random addition</title></head> <body> <p> <?php $first_number
<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变量,但它带来了一个错误,我并没有定义变量,但我认为我已经定义了 你好,朋友,你也曾经
<?php
$a=rand(0,100);
$b=rand(0,100);
$c=$a+$b;
echo $a."+".$b."=".$c;
?>
这里不是做家庭作业的地方。请使用rand函数。。因此,一个编程社区将帮助您解决代码中的问题,它不是一个代码精灵的地方,您为您工作,投入一些精力,通过发布代码让我们知道您遇到了什么问题。rand(0100)+rand(0100)
我想我不记得syntax@User016我不同意。但正如马特·克拉克所说,付出一些努力吧,别指望代码精灵会神奇地为你生成代码。我借鉴了你的想法,做了其他的计算,包括所有人的总数。就我个人而言,我喜欢挑战。和平