我正在做一個實驗,目前正在研究健身功能。問題是我正在做一個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