我正在做一个实验,目前正在研究健身功能。问题是我正在做一个SmallChange()函数来执行它的操作,令人讨厌的是它似乎更新了你给它的变量,但它不应该这么做。Java函数正在更新其输入变量
这里是满级的副本:https://gist.github.com/1710367
38行是问题行。
下面是功能。当我给它solution
作为输入它更新solution
与它做出的小改变,但我无法弄清楚如何或为什么。
任何人都知道我要去哪里错了?它开始伤害我的大脑。
// Edits either the angle or velocity of our solution
// by a small amount (about 1% of the diffrence between max and min)
public static Double[] SmallChange(Double[] sol) {
// Pick a 0 or 1
switch (r.nextInt(2)) {
case 1: // Changing the angle
// The angle change amount
Double angle = (angleLimits[1] - angleLimits[0]) * 0.01;
// Make the change, either + or -
if (r.nextInt(2) == 0) {
sol[0] += angle;
} else {
sol[0] -= angle;
}
// Check it's within the limits
if (sol[0] > angleLimits[1]) {
sol[0] = angleLimits[1];
} else if (sol[0] < angleLimits[0]) {
sol[0] = angleLimits[1];
}
break;
case 0: // Changing the velocity
// The velocity change amount
Double velocity = (velocityLimits[1] - velocityLimits[0]) * 0.01;
// Make the change, either + or -
if (r.nextInt(2) == 0) {
sol[1] += velocity;
} else {
sol[1] -= velocity;
}
// Check it's within the limits
if (sol[1] > velocityLimits[1]) {
sol[1] = velocityLimits[1];
} else if (sol[1] < velocityLimits[0]) {
sol[1] = velocityLimits[1];
}
break;
}
return sol;
}
你想调用你的方法'smallChange',而不是'SmallChange'。 – 2012-01-31 13:09:08
这是因为方法名称通常以SmallCase开头(噢,我做了一个双关!) – 2012-01-31 13:36:00