2017-06-29 108 views
1

这里我说的是用户输入的答案号码,并将其与来自文件的输入进行比较,尽管它们都相同,但是它将一个添加到false而不更正我的代码有什么问题比较来自文件的输入和来自用户php的输入

<body> 
<?php 
$read = file("Questions.txt"); 
$Questions[] = -1; 
$x = 0; 
foreach($read as $line){ 
    $Questions[$x] = $line; 
    $x++; 
} 

$output[] = ''; 
$correct = 0; 
$false = 0; 
$counter = 0; 
$answer = $_POST['answers']; 
for ($b=0;$b<7;$b++){ 
    $output[$b] = ""; 
} 
for ($y = 0;$y<$x;$y++){ 
    $temp = $Questions[$y]; 
    for ($a=0;$a<strlen($temp);$a++){ 
     if ($temp{$a} == "~"){ 
      $counter++; 
     } 
     else { 
      $output[$counter] = $output[$counter] . $temp{$a}; 
     } 
    } 

if (strcmp ($answer[0],$output[6]) == 0){ 
    $correct++; 
} 
else { 
    $false++; 
} 
$counter = 0; 
for ($u=0;$u<7;$u++){ 
     $output[$u] = ""; 
    } 
} 



?> 
</body> 
+0

“虽然他们都是平等的,但它增加了一个错误不能更正”。这几乎不可理解。请花时间详细解释问题。非常简单:作为输入提供了什么价值?你期望输出什么价值?你实际获得什么价值作为输出? – ADyson

+0

这是一个噩梦...... sic'$ temp {$ a}'<<这是什么?什么是输入,我看到的只是'$ x = $ y = $ z!='〜''bla bla bla .... – ArtisticPhoenix

回答

0

这里有很多缺失的信息。 Questions.txt文件中包含什么内容?答案如何发布?

假设(大假设这里)的问题txt文件被分成问题与答案分隔|在每一行上。

例如

>  what is 1+1?|2 
>  how do you spell red|red 

此外,假设用户提供的答案发布为逗号分隔列表。

例如 2,红

如果是这样的话,那么.....

<?php 
$read = file("Questions.txt"); 
$questions = []; 
$answers = []; 

foreach($read as $line){ 
    $qa = explode('|', $line); 
    $questions[] = $qa[0]; 
    $answers[] = $qa[1]; 
} 

$output = []; 
$correct = 0; 
$false = 0; 
$userAnswers = explode(',', $_POST['answers']); 

foreach($userAnswers as $key => $answer){ 
    if($answers[$key] == $answer){ 
     $correct++; 
     $output[] = $questions[$key].' is correct'; 
    }else{ 
     $false++; 
     $output[] = $questions[$key].' is incorrect'; 
    } 
} 

print_r($output); //or whatever 
?> 

如果这些假设是不正确的,也许你可以把它们作为建议?