这是有问题的代码:
public void setReminder() {
if (db.getTotalIncome(prefs.getString("username", "")) != 0.0 ||
db.getTotalExpenses(prefs.getString("username", "")) != 0.0 ||
db.getReminder(prefs.getString("username", "")) != null) {
Float income = db.getTotalIncome(prefs.getString("username", ""));
Float expense = db.getTotalExpenses(prefs.getString("username", ""));
Cursor reminder = db.getReminder(prefs.getString("username", ""));
if (reminder.moveToFirst() != false) {
String reminderValue = reminder.getString(reminder.getColumnIndex("value"));
if (expense <= Integer.parseInt(reminderValue)/100*income) {
PugNotification.with(this)
.load()
.title("Warning")
.message("Expenses nearing threshold")
.bigTextStyle("You have spent more than " + Integer.parseInt(reminderValue) / 100 + "% " + "of your income!")
.smallIcon(R.drawable.icon_flag)
.largeIcon(R.drawable.icon_flag)
.flags(Notification.DEFAULT_ALL)
.simple()
.build();
}
}
}
}
当前,在if (expense <= Integer.parseInt(reminderValue)/100*income)
,它似乎总是正确的,从而导致显示通知。我似乎无法弄清楚发生了什么。另一件事,似乎是第一个if
语句
if (db.getTotalIncome(prefs.getString("username", "")) != 0.0 ||
db.getTotalExpenses(prefs.getString("username", "")) != 0.0 ||
db.getReminder(prefs.getString("username", "")) != null)
也总是返回true。我读到float的默认值为0.0,所以我的代码是错误的还是发生了什么?
答案 0 :(得分:1)
您的代码:
if (db.getTotalIncome(prefs.getString("username", "")) != 0.0 ||
db.getTotalExpenses(prefs.getString("username", "")) != 0.0 ||
db.getReminder(prefs.getString("username", "")) != null)
if内的表达式始终为true,因为您使用的是||
,这意味着or
,因此,如果任何参数为true,则整个表达式为true。在您的情况下,至少一个参数是true
。
答案 1 :(得分:0)
尝试使用
expense.intValue() <= Integer.parseInt(reminderValue)/100*income
与您的其他if语句相同:
if (db.getTotalIncome(prefs.getString("username", "")).intValue() != 0 ||
db.getTotalExpenses(prefs.getString("username", "")).intValue() != 0 ||
db.getReminder(prefs.getString("username", "")) != null)
有关更多信息,请阅读以下答案:https://stackoverflow.com/a/29139595/284308