我在循环中添加,但返回值始终为0,我无法弄清楚。For循环将不会添加,返回原始值(android,java)
如果我取消注释最后2行中的一行,它将正确返回手动值,并且类的其余部分将工作。 ARRAYLIST_SIZE = 10.
public float averageBearing() {
float sumBng = 0;
for (int i = 0; i==ARRAYLIST_SIZE; i++) {
Location l = locList.get(i);
float tempBearing = l.getBearing();
sumBng += tempBearing;
}
float finalBng = sumBng/ARRAYLIST_SIZE;
//remove following lines for real use
//finalBng = (float) (Math.random()*360);
//finalBng = (float) 105.0;
return finalBng;
}
我相当确定列表中的位置有轴承,这里是add方法。现在我必须欺骗方位,因为如果我们正在移动,位置只有它,但我在我的固定桌子上。
public void add(Location location) {
if (locList == null) {
locList = new ArrayList<Location>();
}
//test code to spoof bearing
location.setBearing((float) 105.0);
//use only locations with extra data
if (location.hasBearing() && location.hasSpeed()) {
locList.add(location);
mostRecent = location;
//ensure we have at most 10 recent locations
//no reason to use stale data
while (locList.size()>10) {
locList.remove(0);
}
}
ARRAYLIST_SIZE = locList.size();
}
为什么初始化一个新的tempBearing循环? – ChiefTwoPencils 2013-05-11 20:52:57
我建议你学习如何使用Eclipse或IntelliJ IDEA调试器。这些是任何Android开发人员的必备工具。它们允许您一次一行地查看代码,看看它在做什么。您还可以查看所有变量的值,以确保它们符合您的期望。 – 2013-05-11 21:02:59
我在Eclipse上使用ADT,我会研究它。该软件有这么多工具,我需要花一天时间,只是学习环境... – 2013-05-11 21:07:38