这是我的方法:
public double getCombinedAprThisMonth() {
Cursor c = database.rawQuery("SELECT * FROM debt;", null);
double totalMonthlyFee = 0;
double SingleAprRate = 0;
double[] storeFees;
int rows = c.getCount();
storeFees = new double[c.getCount()];
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
for (int i = 0; i < rows; i++) {
String BalColumn = c.getString(c
.getColumnIndex("debt_total"));
String AprColumn = c.getString(c.getColumnIndex("apr"));
double aprColumn = Double.valueOf(BalColumn);
double balColumn = Double.valueOf(AprColumn);
SingleAprRate = (((aprColumn / 100) / 12) * balColumn);
storeFees[i++] = SingleAprRate;
}
}
for(double i : storeFees) {
totalMonthlyFee += i;
}
c.close();
return totalMonthlyFee;
}
有三个记录,所以三个循环应该是愉快的。 totalMonthlyFee返回90.但是,数据是8.33,45和45.我试图得到它的总和(98.33应该是正确的,但我得到90?)。任何人都能看到在这里拧什么?
答案 0 :(得分:1)
第二个想法,如果我明白你在做什么,我认为你根本不需要内循环。也许试试这个...
int j = 0;
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
String BalColumn = c.getString(c
.getColumnIndex("debt_total"));
String AprColumn = c.getString(c.getColumnIndex("apr"));
double aprColumn = Double.valueOf(BalColumn);
double balColumn = Double.valueOf(AprColumn);
SingleAprRate = (((aprColumn / 100) / 12) * balColumn);
storeFees[j++] = SingleAprRate;
}
答案 1 :(得分:0)
当你已经将循环体的一部分递增时,不要在循环体中增加i。