我写过这个函数:
public function Main() {
kickcount(24);
}
private function kickcount(kick:int){
var kicknr:int = 0;
for(var i:int = 0; i <= kick; i++){
kicknr = kicknr + i;
trace(kicknr);
}
}
输出是这样的:
0
1
3
6
10
15
21
28
36
45
55
66
78
91
105
120
136
153
171
190
210
231
253
276
300
我想重写该功能,以便它只输出300.如何做到这一点?
答案 0 :(得分:1)
在for循环之后放置跟踪。
private function kickcount(kick:int){
var kicknr:int = 0;
for(var i:int = 0; i <= kick; i++){
kicknr = kicknr + i;
}
trace(kicknr);
}
答案 1 :(得分:0)
实现相同输出结果的另一种方法是这样的:
public function Main() {
kickcount(24);
}
private function kickcount(x:int){
var formula = (x * (x / 2) + (x / 2));
trace(formula);
}