【问题标题】:Adding variables in javascript在javascript中添加变量
【发布时间】:2013-09-13 19:37:50
【问题描述】:

我正在寻求帮助。 我希望能够添加 2 个变量,然后将其中一个变量设置为更高的数字。

function gain_points() {
var total_points = parseInt(0)
var points_per_click = parseInt(1)
var points_per_second = parseInt(0)}

我希望能够将 total_points 和 points_per_click 相加,然后再增加 total_points。

这可能吗?

【问题讨论】:

  • 所以把它们加在一起......
  • parseInt(0) 等是多余的;仅仅说0,您将一无所获。 parseInt 用于当您有一个字符串需要改为数字时,并希望确保将其解释为整数而不是浮点数。
  • 你想用points_per_second做什么?

标签: javascript variables web addition


【解决方案1】:

在 gain_points 函数中定义了 var total_points,它将在每次调用该函数时被定义并赋值为 0。

您可能需要考虑这样的事情:

var total_points = parseInt(0);
var points_per_click = parseInt(1);
var points_per_second = parseInt(0);

function gain_points() {
    total_points = total_points + points_per_click;
}

这允许 total_points 在每次调用该函数时继续增加。

你也不需要使用 parseInt();下面的就好了:

var total_points = 0;
var points_per_click = 1;
var points_per_second = 0;

【讨论】:

    【解决方案2】:
    total_points = total_points + points_per_click;
    

    这是你说的吗??

    对不起,不能评论,没有足够的代表......

    【讨论】:

      猜你喜欢
      • 2011-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-06
      • 2015-02-22
      • 2019-09-06
      • 2012-04-20
      • 2015-03-23
      相关资源
      最近更新 更多