2016-08-23 69 views
-5

在我的應用程序中,我要求用戶提供他們的小時費率,然後我有一個計時錶,我想弄清楚他們自從時間開始以來賺了多少。要做到這一點,我需要把他們的小時費率變成秒鐘,再乘以天文臺的秒數。爪哇分區與整型括號

 timer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() 
    { 
     @Override 
     public void onChronometerTick(Chronometer chronometer){ 
      long timeElapsed = SystemClock.elapsedRealtime() - timer.getBase(); 
      int hours = (int) (timeElapsed/3600000); 
      int minutes = (int) (timeElapsed - hours * 3600000)/60000; 
      int seconds = (int) (timeElapsed - hours * 3600000 - minutes * 60000)/1000; 

      int hrlyRate = Integer.parseInt(hourlyRate.getText().toString()); 

      secondsRate = (hrlyRate/60)/60; 
      moneyEarned = secondsRate * seconds; 

      Log.d("hrlyRate", Integer.toString(hrlyRate)); 
      Log.d("secondsRate", Integer.toString(secondsRate)); 


     } 
    }); 

假設用戶輸入40到hourlyRate字段這是我從一個EditText得到的,爲什麼是secondsRate = 0的計算?不應該(40/60)/ 60 = $ 0.01 /秒?

回答

0

嘗試此(使用浮動/雙代替int和60.0f/60.0而不是60,例如)

timer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() 
{ 
    @Override 
    public void onChronometerTick(Chronometer chronometer){ 
     double timeElapsed = SystemClock.elapsedRealtime() - timer.getBase(); 
     double hours = (double) (timeElapsed/3600000.0); 
     double minutes = (double) (timeElapsed - hours * 3600000.0)/60000.0; 
     double seconds = (double) (timeElapsed - hours * 3600000.0 - minutes * 60000.0)/1000; 

     double hrlyRate = Double.parseDouble(hourlyRate.getText().toString()); 

     secondsRate = (hrlyRate/60.0)/60.0; 
     moneyEarned = secondsRate * seconds; 

     Log.d("hrlyRate", Double.toString(hrlyRate)); 
     Log.d("secondsRate", Double.toString(secondsRate)); 


    } 
});