【问题标题】:Why isnt my integer coming up as 0 instead of the value i set in a count down timer?为什么我的整数不是 0 而不是我在倒数计时器中设置的值?
【发布时间】:2014-10-04 19:19:31
【问题描述】:

在下面的代码中,我有一个名为 ship1 的整数,它的值在倒数计时器对象中设置为 5。我想将此 int 值放入一个字符串数组,但是当我这样做并将字符串数组的第一行打印到文本视图中时,ship1AddSpend(整数)出现为零。为什么值没有变为 5?有人可以帮忙吗?

final TimerClass Timer2 = (TimerClass) new TimerClass(500,1000) {

    final public int OnFinish()
    {
        ship1AddSpend = 5;
        this.start();
        return ship1AddSpend;
    }

}.start();

int ship1AddSpend;

        final String[] shipDesc = {
                "10 Planets Every 5 Secs \n" + ship1AddSpend + "$",
                "50 Planets Every 5 Secs \n",
                "100 Planets Every 5 Secs \n 1500$",
                "500 Planets Every 4 Secs \n 3000$",
                "1000 Planets Every 4 Secs \n 7500$",
                "5000 Planets Every 4 Secs \n 15000$",
                "10000 Planets Every 3 Secs \n 50000$",
                "30000 Planets Every 3 Secs \n 100000$",
                "60000 Planets Every 3 Secs \n  500000$",
                "100000 Planets Every 1 Secs \n 1000000$"};

【问题讨论】:

  • 因为你没有同步。
  • 这是什么意思,你能解释一下你的答案吗?
  • I have an integer called ship1。在哪里? when I print the first line of the string array。你什么时候这样做?你知道int ship1AddSpend;。将其更改为int ship1AddSpend=33;。然后告诉我们您现在获得的价值。

标签: java android timer int


【解决方案1】:

我从您的代码中了解到您正在初始化字符串数组;特别是第 0 位的字符串,其原始值为 ship1AddSpend,即 0。一旦字符串初始化,它的值将永远不会改变,因为它是不可变的。

您可能想要做的是在所需变量的值更改后准备字符串,或者在您想要使用/显示它时准备字符串。使用StringBuffer 动态准备字符串。希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2019-12-07
    • 1970-01-01
    • 1970-01-01
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多