2013-05-28 63 views
-2

下面的代碼在嘗試保存分數時會給出奇怪的結果(它會像if語句失敗一樣添加大量值)。當在if語句中進行比較時,超出範圍

double EndScore = (double)(mScoreManager.getScore()); 

    if(mWorldCounterBox.getCurrentAlivePopulationPercent() < 97.0) 
    { 
     EndScore += (mWorldCounterBox.getCurrentAlivePopulationPercent()) * 100.00; 
    } 

    System.out.println(mWorldCounterBox.getCurrentAlivePopulationPercent()); //Output: 1.0544652507298142E-7 
    System.out.println(EndScore); //Output: 1.0000105446525074 

    //Increase score per dead %. 
    if(mWorldCounterBox.getCurrentAlivePopulationPercent() < 0.1) 
    { 
     EndScore += 20000.0; 
    } 
    System.out.println(EndScore); //Outout 20001.000010544652 -- WRONG HERE 
    SettingsSaveLoad.setCountryScore(mCurrentWorld, (float)(EndScore)); 
+4

我不理解你的問題。 'mWorldCounterBox.getCurrentAlivePopulationPercent()'小於0.1,所以20000.0被添加到'EndScore'。 –

+0

儲蓄分數在哪裏? – PeterMmm

+0

EndScore的價值應該是什麼,順便說一句,您應該按照慣例重新命名爲endScore? – Djon

回答

1

System.out.println(mWorldCounterBox.getCurrentAlivePopulationPercent()); //輸出:1.0544652507298142E-7

裝置mWorldCounterBox.getCurrentAlivePopulationPercent()1.0544652507298142 * ((10)^-7) 其是小於0.1

SO

EndScore += 20000.0; //1.0000105446525074+ 20000.0 

EndScore後變得20001.000010544652

1

滿足兩個時間的條件。

如果值小於0.1的也小於97.0

你把你的條件不對我猜

mWorldCounterBox.getCurrentAlivePopulationPercent()小於0.1。

如果第一個條件滿足

if(mWorldCounterBox.getCurrentAlivePopulationPercent() < 97.0) //mWorldCounterBox.getCurrentAlivePopulationPercent() <97.0 true 

它總是在第二條件也

if(mWorldCounterBox.getCurrentAlivePopulationPercent() < 0.1) //mWorldCounterBox.getCurrentAlivePopulationPercent() <0.1 true 

側注:

請遵循Java命名conventions.Ex:EndScore應成爲endScore