当我在倒计时器中将其设置为5时,为什么我的整数值为零

时间:2014-10-05 09:13:14

标签: java android arrays

下面的代码中,我有一个名为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$"};

3 个答案:

答案 0 :(得分:1)

仅在计时器结束时设置ship1AddSpend值。你也需要给它一个初始值,如:

int ship1AddSpend = 5;

答案 1 :(得分:0)

您在计时器结束前正在读取值。只有在OnFinish()执行后才能读取它。

答案 2 :(得分:-1)

将int shipAddSpend移动到up将解决您的问题

int ship1AddSpend;

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

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

}.start();



    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$"};