我希望 if 语句在 12 分钟或过去 12 分钟时运行,但我不断收到错误"类型不匹配:无法从 int 转换为布尔值",我该如何解决这个问题?
public void clock() {
Thread cloo = new Thread() {
public void run() {
try {
while (true) {
Calendar cal = new GregorianCalendar();
int day = cal.get(Calendar.DAY_OF_MONTH);
int hour = cal.get(Calendar.HOUR);
int min = cal.get(Calendar.MINUTE);
int second = cal.get(Calendar.SECOND);
lblNewLabel_1.setText(day + "/ " + hour + ":" + min + ":" + second);
sleep(1000);
if (min =+ 12) { // <<<<< where the problem is
message.setText("its been over 12 minutes");
}
}
}
}
}
}
if
表达式只接受boolean
值,但min =+ 12
是int
值,因此会发生类型不匹配。
试试这个:
int countdown = 12;
while (true) {
Calendar cal = new GregorianCalendar();
int day = cal.get(Calendar.DAY_OF_MONTH);
int hour = cal.get(Calendar.HOUR);
int min = cal.get(Calendar.MINUTE);
int second = cal.get(Calendar.SECOND);
lblNewLabel_1.setText(day + "/ " + hour + ":" + min + ":" + second);
sleep(1000);
countdown = countdown - 1;
if (countdown <= 0) {
message.setText("its been over 12 minutes");
}
}
此外,根据您的编程设计,您应该决定当时间超过 12 分钟时该怎么做,停止它或重新倒计时?上面的代码将重复执行message.setText("its been over 12 minutes");
。