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%),而不是每個項目更新由給定的百分比,它似乎劃分數組內的項目之間的百分比,給我醜陋的數字。通過以給定百分比增加來傳遞和更新分數陣列
任何援助將大大appreicated!
如果你想單獨更新每個分數,你爲什麼有獲取每個分數增加一個'sum'變量(由以前的總和)? – UnholySheep
constrain()在代碼中的作用 –
如果'sum == 0',我在代碼 – copernicon1543