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%)で配列を正しく更新しているようですが、各パーツを与えられたパーセンテージで更新するのではなく、配列内のアイテム間でパーセンテージを分けているようです。与えられたパーセンテージでスコアの配列を増やして更新します。
大変助かりました。
: は、私はそうのような何かをしますか? – UnholySheep
コード内のconstrain()の役割 –
'sum == 0 'の場合、 – copernicon1543