首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >循环浮点数为整数

循环浮点数为整数
EN

Stack Overflow用户
提问于 2022-07-31 17:28:29
回答 2查看 30关注 0票数 0

对于循环浮点数到整型Hello,我如何将浮动的thanos上的这个For循环更改为整数?

代码语言:javascript
复制
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,以便输出

代码语言:javascript
复制
1 1
2 3
3 1
4 3
5 9

谢谢。

EN

回答 2

Stack Overflow用户

发布于 2022-07-31 17:32:35

Math.floor()返回一个新的数字,不修改旧的数字

代码语言:javascript
复制
thanos = Math.floor(thanos)

此外,请记住声明变量。我会这样写你的代码:

代码语言:javascript
复制
let thanos = 1
for(let day = 2; day < 51; day += 1){
  if(day % 3 == 0){      
    thanos = Math.floor(thanos / 2)
  } else {
    thanos *= 3
  }
}
票数 1
EN

Stack Overflow用户

发布于 2022-07-31 17:36:12

这将输出您想要的结果。

代码语言:javascript
复制
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)
}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73185505

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档