获得两个给定数字之间的4个数字(步长)

时间:2015-07-24 08:16:46

标签: php

我正在尝试使用此代码

在两个给定数字之间获取 synchronized void saveScores(final Context contex, final String scoreOneKey, final String scoreTwoKey, final Integer scoreOne, final Integer scoreTwo){ final SharedPreferences sharedPreferences = context.getSharedPreferences("user.score.key", Context.MODE_PRIVATE | Context.MODE_MULTI_PROCESS) final SharedPreferences.Editor preferencesEditor = sharedPreferences.edit(); preferencesEditor.putInt(scoreOneKey, scoreOne); preferencesEditor.putInt(scoreTwoKey, scoreTwo); preferencesEditor.commit(); } 步数
4

当两个给定为(0,100)时,代码正常工作,因为<?php function getNums($num1, $num2){ $diff = $num2 -$num1; $steps =[]; $steps[0] = $diff/4; $steps[1] = $diff/2; $steps[2] = round($num2/1.33333333333); $steps[4] = $num2; foreach($steps as $step) { echo $step; echo "<br>"; } } 返回:

getNums(0, 100);

但是当数字类似25 50 75 100 时,输出就像

getNums(50, 100);

你能否告诉我为什么会这样?以及如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

您需要将分部的结果添加到$num1

function getNums($num1, $num2){
    $diff = $num2 -$num1;
    $steps =[
        round($num1 + $diff/4), 
        round($num1 + $diff/2), 
        round($num1 + $diff*.75), 
        $num2
    ];

    foreach($steps as $step) {
        echo $step;
        echo "<br>";
   }
}