我正在尝试进行一项测验,该测验会向接受它的人提供他们得到的答案数量的平均值。例如:如果他们有2个权利,它将显示66%。有人可以向我解释我如何做这样的事情。
<!DOCTYPE html>
<html>
<head>
<title>Question</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
</head>
<body style="background-color:lightblue">
<h1 style="text-align:center;color:white"> Answer these Questions</h1>
<div class="container" style="margin-top: 50px">
<?php
$numcorrect=0;
// If the submit button has been pressed
if(isset($_POST['submit']))
{
// Check answer
if($_POST['answer'] == 21)
{
$numcorrect= ($numcorrect + 1);
}
if($_POST['answer1'] == 46)
{
$numcorrect= ($numcorrect + 1);
}
if($_POST['answer2'] == 2468)
{
$numcorrect= ($numcorrect + 1);
}
$average=$numcorrect/count ($numcorrect);
echo "<h1>Your Average is {$_POST['$Average']}</h1>";
}
?>
<!-- Quiz Form -->
<form method="post" action="questionassign1.php">
<ol>
<div class="form-group">
<h3><li>How old am I ?</li></h3>
<input name="answer" type="text" class="form-control" />
<h3><li>How old is my Mom?</li></h3>
<input name="answer1" type="text" class="form-control" />
<h3><li>What is my favorite number?</li></h3>
<input name="answer2" type="text" class="form-control" />
</li>
</div>
</ol>
<input name="submit" type="submit" value="Check answer" class="btn btn-primary" />
</form>
</div>
</body>
</html>
答案 0 :(得分:0)
使用此代码:
<?php
$numcorrect = 0;
// If the submit button has been pressed
if (isset($_POST['submit'])) {
// Check answer
if ($_POST['answer'] == 21) { {
$numcorrect++;
}
if ($_POST['answer1'] == 46) {
$numcorrect++;
}
if ($_POST['answer2'] == 2468) {
$numcorrect++;
}
$average = ($numcorrect/3)*100;
echo "<h1>Your Average is $average %</h1>";
}
}
?>
:)
答案 1 :(得分:0)
$average=$numcorrect/count ($numcorrect);
echo "<h1>Your Average is ".$average."%</h1>";
您已经计算了平均值,因此请将其放在带有.
字符串concat运算符的字符串中。附加提示:检查您的代码,如果我不知道任何答案会发生什么?