对于循环浮点数到整型Hello,我如何将浮动的thanos上的这个For循环更改为整数?
thanos = 1
for(let day =1; day < 51; day){
console.log(day, thanos)
day++
Math.floor(thanos)
if(day % 3 == 0){
thanos /= 2
}
else{
thanos *= 3
}
}
The output is
1 1
2 3
3 1.5
4 4.5
5 13.5如何将1.5 *= 3的thanos *= 3变成1 *= 3,以便输出
1 1
2 3
3 1
4 3
5 9谢谢。
发布于 2022-07-31 17:32:35
Math.floor()返回一个新的数字,不修改旧的数字
thanos = Math.floor(thanos)此外,请记住声明变量。我会这样写你的代码:
let thanos = 1
for(let day = 2; day < 51; day += 1){
if(day % 3 == 0){
thanos = Math.floor(thanos / 2)
} else {
thanos *= 3
}
}发布于 2022-07-31 17:36:12
这将输出您想要的结果。
thanos = 1
for(let day =1; day < 51; day){
console.log(day, thanos)
day++
if(day % 3 == 0){
thanos /= 2
} else {
thanos *= 3
}
thanos = Math.floor(thanos)
}
https://stackoverflow.com/questions/73185505
复制相似问题