亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何計算php中具有不同值的文件的匹配項數量

如何計算php中具有不同值的文件的匹配項數量

PHP
至尊寶的傳說 2022-08-05 09:32:43
對于多項選擇考試,我將數據存儲在文件中。.txt我有8個文件,其中存儲了2個或更多數字。txt 文件的名稱是.txt1.txt, 2.txt, 3.txt ...每個 txt 文件中的第一個數字始終是正確的答案。我想將第一個數字與該txt文件的最后一個數字進行比較。foreach($all_files as $file) {    $each_file = file_get_contents($file);    $choosen_answer = substr($each_file, -1); // last number is the number user has choosen    $correct_answer = substr($each_file, 0,1); // first number is the correct answer    // output the answers    echo '<span class="choosen_answer">'.$choosen_answer.'</span>';     echo '<span class="correct_answer">'.$correct_answer.'</span>';    $wrong = 0;    if($choosen_answer != $correct_answer) { // compare the 2 values        $wrong++;        echo '<span class="wrong text-danger">Wrong</span>';    }    else {        echo '<span class="correct text-success">Correct</span>';    }}echo count($wrong); // this should give me the number of wrong answers, but it does not...我想計算錯誤答案的數量,我為此嘗試了,但它沒有給我錯誤答案的數量。count($wrong);所以我需要什么:如果問題回答錯誤,它應該給我數字383每個 txt 文件如下所示:02 // 0 is the correct answer, 2 is the answer from the user. I compare 0 with 2. So wrong answer或14 // 1 is the correct answer. 4 is the answer of the user. I compare 1 with 4. So wrong answer或22 // 2 is the correct answer. 2 is also the answer of the user. So correct answer
查看完整描述

1 回答

?
aluckdog

TA貢獻1847條經驗 獲得超7個贊

$wrong = 0;在前循環中!因此,在每次迭代之后,它再次使其為0。


您的代碼應如下所示:


$wrong = 0;

foreach($all_files as $file) {

    $each_file = file_get_contents($file);

    $choosen_answer = substr($each_file, -1); // last number is the number user has choosen

    $correct_answer = substr($each_file, 0,1); // first number is the correct answer


    // output the answers

    echo '<span class="choosen_answer">'.$choosen_answer.'</span>'; 

    echo '<span class="correct_answer">'.$correct_answer.'</span>';


    if($choosen_answer != $correct_answer) { // compare the 2 values

        $wrong++;

        echo '<span class="wrong text-danger">Wrong</span>';

    }

    else {

        echo '<span class="correct text-success">Correct</span>';

    }


}

echo $wrong; 


查看完整回答
反對 回復 2022-08-05
  • 1 回答
  • 0 關注
  • 110 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號