gpt4 book ai didi

java - 数组未在 Java/Android 中正确插入数据

转载 作者:行者123 更新时间:2023-11-30 04:12:27 24 4
gpt4 key购买 nike

我有一个方法:

 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?)。有人看到这里有什么问题吗?

最佳答案

再三考虑,如果我理解您在做什么,我认为您根本不需要内部循环。也许试试这个...

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;

}

关于java - 数组未在 Java/Android 中正确插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10629480/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com