2017-03-16 1 views
0
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%)で配列を正しく更新しているようですが、各パーツを与えられたパーセンテージで更新するのではなく、配列内のアイテム間でパーセンテージを分けているようです。与えられたパーセンテージでスコアの配列を増やして更新します。

大変助かりました。

+1

: は、私はそうのような何かをしますか? – UnholySheep

+0

コード内のconstrain()の役割 –

+0

'sum == 0 'の場合、 – copernicon1543

答えて

1

なぜループ内のパーセンテージの値を変更していますか?あなたは、それぞれが個別にスコアを更新する場合は、なぜあなたは(前の合計によって)各スコアのために増加します `sum`変数を持っているん

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

私のロジックはループ内で常にパーセンテージを更新していましたか? – copernicon1543

+0

@ copernicon1543はい.................... –

0
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); 
     } 

    } 
+1

説明のないジャストコードは良い答えとはみなされません。また、未使用変数 'double sum = 0;'の目的は何ですか? – UnholySheep

+0

@UnholySheep "sum"変数は、私がそれを変更する前の古いコードでした。あなたはその論理に疑問を持って正しいです。 – copernicon1543

+0

皆さんの助けをいただきありがとうございます。 – copernicon1543

関連する問題