dongta5747 2015-09-23 13:52
浏览 90
已采纳

如何计算2个数组的比较百分比?

How can I get a percentage of right answers if I have 2 arrays in PHP or javascript, preferably in PHP?

So, I have these two arrays and I want to compare a quiz result with the correct answers and get a percentage score:

$quiz_results = array( 'q1' => 'no',
                    'q2' => 'yes',
                    'q3' => 'no',
)

$answers = array( 1 => 'yes',
                  2 => 'no',
                  3 => 'yes'
)
  • 写回答

2条回答 默认 最新

  • dqjl0906 2015-09-23 13:58
    关注

    Run through the answers and compare them to the questions. If they are the same increment the correct answer count.

    $quiz_results = array( 'q1' => 'yes',
                        'q2' => 'yes',
                        'q3' => 'no',
    );
    $answers = array( 1 => 'yes',
                      2 => 'no',
                      3 => 'yes'
    );
    
    $totalquestions = count($answers);
    $correct = 0;
    foreach($answers as $key => $answer){
        //q + the key should do it. Its easier if they are the same obviously
        if($answer == $quiz_results['q'.$key]){
            // correct 
            $correct++;
        }
    }
    
    echo 100 / $totalquestions * $correct; //returns 33.333333%
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 求一位精通京东相关开发的专家
  • ¥100 求懂行的大ge给小di解答下!
  • ¥15 pcl运行在qt msvc2019环境运行效率低于visual studio 2019
  • ¥15 MAUI,Zxing扫码,华为手机没反应。可提高悬赏
  • ¥15 python运行报错 ModuleNotFoundError: No module named 'torch'
  • ¥100 华为手机私有App后台保活
  • ¥15 sqlserver中加密的密码字段查询问题
  • ¥20 有谁能看看我coe文件到底哪儿有问题吗?
  • ¥20 我的这个coe文件到底哪儿出问题了
  • ¥15 matlab使用自定义函数时一直报错输入参数过多