我正在尝试构建一个在计时器运行时进行算术运算的Android应用程序。我该怎么办?
这是计算类
public class CalculateCharges {
private Double total = 0.0;
private long secs,mins,hrs;
public Double calculateTotal(long hours, long mins) {
if (mins < 1) {
return total = 0.05;
}
if (mins == 1 || (mins > 1 && mins < 2)) {
return total = total + 0.25;
}
return null;
}
}
这是MainActivity类
public void startClick (View view) {
showStopButton();
if (stopped) {
startTime = System.currentTimeMillis() - elapsedTime;
}
else {
startTime = System.currentTimeMillis();
}
mHandler.removeCallbacks(startTimer);
mHandler.postDelayed(startTimer, 0);
}
这是更新计时器
private void updateTimer (float time) {
secs = (long)(time/1000);
mins = (long)((time/1000)/60);
hrs = (long)(((time/1000)/60)/60);
/* Convert the seconds to String * and format to ensure it has * a leading zero when required */
secs = secs % 60;
seconds = String.valueOf(secs);
if (secs == 0) {
seconds = "00";
}
if (secs < 10 && secs > 0) {
seconds = "0" + seconds;
}
/* Convert the minutes to String and format the String */
mins = mins % 60;
minutes = String.valueOf(mins);
if (mins == 0) {
minutes = "00";
}
if (mins < 10 && mins > 0) {
minutes = "0" + minutes;
}
/* Convert the hours to String and format the String */
hours = String.valueOf(hrs);
if (hrs == 0) {
hours = "00";
}
if (hrs < 10 && hrs > 0) {
hours = "0" + hours;
}
((TextView)findViewById(R.id.timer)).setText(hours + ":" + minutes + ":" + seconds);
Double total = calc.calculateTotal(hrs, mins);
((TextView)findViewById(R.id.displayTotal)).setText(total.toString());
}
这是可运行的
private Runnable startTimer = new Runnable() {
public void run() {
elapsedTime = System.currentTimeMillis() - startTime;
updateTimer(elapsedTime);
mHandler.postDelayed(this, 100);
}
};
当我运行代码时,它只显示0.05美元,但1分钟后不会更新。
如果你可以帮我解决这个问题或指导我如何解决这个问题,那会很有帮助。提前谢谢。
答案 0 :(得分:0)
您在此处有错误:
if (mins == 1 || (mins > 1 && mins < 2)) {
return total = total + 0.25;
}
作为long
时,分钟不能大于1且小于2。它不是double
或float
。
无论如何,将你的班级改为:
public class CalculateCharges {
private Double total = 0.0;
private long secs,mins,hrs;
public Double calculateTotal(long hours, long mins) {
if (mins < 1) {
total = 0.05;
} else if (mins == 1) {
total = total + 0.25;
}
return total;
}
}