duanmuyao0463 2014-05-14 12:48
浏览 34
已采纳

计算间隔之间的进度百分比[关闭]

I'm creating a game, where user can do some progress to move to new levels. Here is the code:

<?php
    $progress=150; // example
    $percentage=0;
    $level=0;

    if ($progress<300) { $level = 1; $percentage=; }
    if ($progress<600) { $level = 2 $percentage=; }
    if ($progress<900) { $level = 3 $percentage=; }

    echo $percentage;
?>

So if the progress is 150 pts for example, the level of this user is 1, and percentage he did to move to new level 2, is 50% (done).

For example if progress is 750 pts, the progress should show 50% (is done) to move to new level 3.

I choosed here simple values, 150 and 750, but I don't know how to calculate if progress is 610, 625 etc...

Let me know if you have any questions.

Thanks.

  • 写回答

5条回答 默认 最新

  • dsjfrkvn818747 2014-05-14 12:59
    关注

    You should store the progresses as an array, and loop over them:

    $progress = 750;
    $levels = array(0, 300, 600, 900);
    
    foreach ($levels as $key => $threshold) {
        if ($progress >= $threshold) {
            $level = $key;
    
            if (isset($levels[$key + 1])) {
                $percentage = 100 * ($progress - $threshold) /
                              ($levels[$key + 1] - $threshold);
            } else {
                // user has passed the final level
                $percentage = null;
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容