我必须编写完整的代码以进行10个问题的测验。我通过此代码比较并准备结果。是否有任何解决方案,以便我可以键入此“if if else if else else”仅阻止一次并对此应用循环并获得相同的结果?
$count=0;
$count2=0;
if($ans1==$answer1)
{
$res1= "Correct";
$count=$count+1;
}
else if($answer1=="")
{
$res1="Not attempted";
}
else
{
$res1= "Incorrect";
$count2=$count2-1;
}
if($ans2==$answer2)
{
$res2= "Correct";
$count=$count+1;
}
else if($answer2=="")
{
$res2="Not attempted";
}
else
{
$res2= "Incorrect";
$count2=$count2-1;
}
if($ans3==$answer3)
{
$res3= "Correct";
$count=$count+1;
}
else if($answer3=="")
{
$res3="Not attempted";
}
else
{
$res3= "Incorrect";
$count2=$count2-1;
}
if($ans4==$answer4)
{
$res4= "Correct";
$count=$count+1;
}
else if($answer4=="")
{
$res4="Not attempted";
}
else
{
$res4= "Incorrect";
$count2=$count2-1;
}
if($ans5==$answer5)
{
$res5= "Correct";
$count=$count+1;
}
else if($answer5=="")
{
$res5="Not attempted";
}
else
{
$res5= "Incorrect";
$count2=$count2-1;
}
if($ans6==$answer6)
{
$res6= "Correct";
$count=$count+1;
}
else if($answer6=="")
{
$res6="Not attempted";
}
else
{
$res6= "Incorrect";
$count2=$count2-1;
}
if($ans7==$answer7)
{
$res7= "Correct";
$count=$count+1;
}
else if($answer7=="")
{
$res7="Not attempted";
}
else
{
$res7= "Incorrect";
$count2=$count2-1;
}
if($ans8==$answer8)
{
$res8= "Correct";
$count=$count+1;
}
else if($answer8=="")
{
$res8="Not attempted";
}
else
{
$res8= "Incorrect";
$count2=$count2-1;
}
if($ans9==$answer9)
{
$res9= "Correct";
$count=$count+1;
}
else if($answer9=="")
{
$res9="Not attempted";
}
else
{
$res9= "Incorrect";
$count2=$count2-1;
}
if($ans10==$answer10)
{
$res10= "Correct";
$count=$count+1;
}
else if($answer10=="")
{
$res10="Not attempted";
}
else
{
$res10= "Incorrect";
$count2=$count2-1;
}
//INCREASE POINTS IN FOLLOWING STATEMENT ONLY USING 2*$COUNT
$finalpoints=$count+$count2;
$_SESSION["pointsession"]= $finalpoints;
答案 0 :(得分:2)
使用数组存储数据:
$count=0;
$count2=0;
$ans = array($answer1,$answer2,$answer3,...);
$answer = array($ans1,$ans2,$ans3,...);
for($i=0;$i<10;$i++){
if($ans[$i]==$answer[$i])
{
$res[$i]= "Correct";
$count=$count+1;
}
else if($answer[$i]=="")
{
$res[$i]="Not attempted";
}
else
{
$res[$i]= "Incorrect";
$count--;
}
}
//INCREASE POINTS IN FOLLOWING STATEMENT ONLY USING 2*$COUNT
$finalpoints=$count+$count2;
$_SESSION["pointsession"]= $finalpoints;
答案 1 :(得分:0)
您可以使用数组
执行此类操作$count=0;
$count2=0;
var ans = [$ans1, $ans2.... ];
var answer = [$answer1, $answer2.... ];
var res = [];
answer.forEach(function($answer, i) {
if(ans[i]==$answer)
{
res.push("Correct");
$count=$count+1;
}
else if($answer=="")
{
res.push("Not attempted");
}
else
{
res.push("Incorrect");
$count2=$count2-1;
}
});
$res1 = res[0];
$res2 = res[1];
...
请注意,...表示您必须填写的代码
答案 2 :(得分:0)
再次,假设问题和答案都在数组中
$count=0;
$ans=array(1,2,3,4,23,1,100);
$answer=array(1,2,'',4,'',54,100);
function mark($answer,$correct){
if( $answer==$correct ) $rv=array('response'=>'correct','score'=>1, 'correct'=>$correct, 'answer'=>$answer );
elseif( $answer=='' ) $rv=array('response'=>'not attempted','score'=>0, 'correct'=>$correct, 'answer'=>$answer );
else $rv=array('response'=>'incorrect','score'=>-1, 'correct'=>$correct, 'answer'=>$answer );
return (object)$rv;
}
for( $i=0; $i < count( $ans ); $i++ ){
$result=mark( $answer[$i], $ans[$i] );
$count += $result->score;
echo 'Question '.( $i+1 ).': Correct answer:' . $result->correct.' Your answer:'.$result->answer.' - '.$result->response . ' - score:'.$result->score.'<br />';
}
echo 'Final score: '.$count.' / '.count($answer);