通过按给定百分比增加来传递和更新分数数组

Passing and updating an array of scores by increasing with given percentage

public class ScoreCard {

private double[] scores;
/**
 * 
 * @param val
 * @param low
 * @param high
 * @return low if val < low, 
 * high if val > high, 
 * val if val is between low and high
 */
private double constrain(double val, int low, int high) {
    if (val < low)
        return low;
    if (val > high)
        return high;
    return val;
    }

.
.
.
.

/**
 * update each score so it increases by given percentage. For example,
 * if score = {60.0, 90.0} before the method is called, it should
 * become {72.0, 100.0} after the method is called with parameter 20.
 * Note: 90.0 increased by 20% is 108, but scores should be constrained between
 * 0 and 100. So, 100.
 * @param percentage
 */

public void scale(double percentage) {
    for (int i = 0; i < scores.length; i++) {
        percentage = scores[i] / 100.0 * percentage;
        scores[i] += constrain(percentage, 0, 100);
        }
    }

我又被项目的一小段代码卡住了。当我尝试通过此函数时,我没有通过 JUnit 测试。它似乎以给定的百分比 (10%) 正确地更新了数组,但不是每个项目都按给定的百分比更新,它似乎在数组内的项目之间划分百分比,给我丑陋的数字。

如有任何帮助,我们将不胜感激!

public void scale(double percentage) {
    for (int i = 0; i < scores.length; i++) {
        percentage = scores[i] / 100.0 * percentage;

        scores[i] =(scores[i]+percentage)>=100?100:(scores[i]+percentage);
        }

    }

为什么要更改循环中的百分比值? 我会这样做:

for (int i = 0; i < scores.length; i++) {
    scores[i] = constrain(scores[i]*(1+percentage/100), 0, 100);
}