我是PHP的新手,並且正在嘗試編寫一個程序,該程序將生成一系列加法總和(涉及兩個隨機生成兩個數字,從0到100)。然後需要讓用戶知道他們是否成功,並詢問他們是否想繼續。我通過網站搜索,但似乎沒有類似的答案。預先感謝您1一些幫助使用用戶輸入我的if else語句
這是我迄今爲止所做的;
<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變量,但它帶來了錯誤,我沒有定義變量,但我認爲我有。
這是不是地方做你的家庭作業.. –
使用rand函數.. http://php.net/manual/en/function.rand.php –
SO是一個編程社區,它可以幫助你解決代碼中的問題,它不是一個代碼精靈,你是否爲你工作,爲此付出一些努力,通過發佈你的問題讓我們知道你有什麼問題碼。 –