【发布时间】:2017-03-11 10:55:21
【问题描述】:
public class ScoreCard { strong text
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;
else
return val;
}
/**
* DEEP copy m into scores with each item contrained between 0 and 100.
* use method {@link this#constrain(double, int, int)}.
* For example, if s = {-15.2, 67.4, 126.8}, scores should become
* {0, 67.4, 100}, AND scores should be a DEEP copy of s.
* @param s (assume s is not null)
*/
public void setMarks(double[] s) {
for(int i = 0; i < s.length; i++) {
if (s[i] < 0 || s[i] > 100)
constrain(s[i], 0, 100);
this.scores[i] = s[i];
}
}
我已经在这部分代码上停留了一段时间。正如 Javadoc 所述,我无法将“constrain”的参数调用为“setMarks”来设置 100 到 100 的分数。我也不认为我的代码正确地创建了深层副本正确地将“s”转换为“scores”。
我们将不胜感激任何朝着正确方向的推动。
【问题讨论】:
-
你调用了 constrain(),但是你忽略了方法返回的约束值。
标签: java copy parameter-passing