2014-10-05 50 views
0

下面的代碼中,我有一個名爲SHIP1整數,其值在倒數計時器對象設置爲5。我想把這個int值放到一個字符串數組中,但是當我將字符串數組的第一行打印到文本視圖中時,ship1AddSpend(整數)就會變成零。爲什麼值不變爲5?有人可以幫忙嗎?爲什麼我的整數值爲零時,我在計數它設置爲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$"}; 
+1

如果將變量聲明爲volatile類型,會發生什麼? – chrylis 2014-10-05 10:07:16

+0

爲什麼在24小時內你有兩個同樣的問題的帖子? – greenapps 2014-10-05 11:23:49

+0

,因爲第一個沒有得到答案 – 2014-10-05 16:04:01

回答

-1

移動INT shipAddSpend到了將解決您的問題

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

不,不會,這個舉動對代碼行爲沒有任何影響,變量仍然有相同的範圍。 – Okas 2014-10-05 09:20:21

1

要設置ship1AddSpend價值,只有當你的計時器結束。你也需要給它一個初始值,如:

int ship1AddSpend = 5; 
+0

這沒有奏效。我將整數設置爲3,但當定時器完成後它仍未更新到5 – 2014-10-05 14:25:09

+0

您必須發佈更多代碼。您目前的代碼片段太短,無法理解您想實現的目標。 – Okas 2014-10-05 14:45:10

0

你當然在讀取計時器結束前的值。僅在OnFinish()執行後才能讀取它。

+0

那麼如何在定時器完成後讓字符串的值爲 – 2014-10-05 14:39:55

+0

您的代碼是可以的。但你應該在更好的時候執行它。 – greenapps 2014-10-05 15:06:21

相關問題