【发布时间】:2015-05-25 14:45:40
【问题描述】:
这是计算平均值的代码,当我运行我的项目时出现 NaN 错误
public static double calculateAverage(){
double attendence = 0;
int no_of_matches = 0;
double average =0;
for (Team t: ApplicationModel.getTeamList()){
for(Match m : ApplicationModel.getMatchList()){
if(m.getTeamname().equals(t.getName())){
attendence =+ (m.getAttendence());
no_of_matches ++;
}
}
average = attendence / no_of_matches ;
}
return average;
}
这是调用计算平均值方法的代码
String[] columnNames = {"Name","Coaches","League","Division","Fulltime","Number of Coaches","Average Attendence"};
if (ApplicationModel.getTeamList()!=null){
int arrayIndex=0;
for (Team c :ApplicationModel.getTeamList()){
String[] currentRow = new String[7];
currentRow[0] = c.getNameAsString();
currentRow[1] = c.getCoachesAsString();
currentRow[2] = c.getLeague();
currentRow[3] = c.getDivision();
currentRow[4] = c.getFulltime();
currentRow[5] = Integer.toString(c.getCoaches().length);
currentRow[6] = Double.toString(c.calculateAverage());
rowInfo[arrayIndex]=currentRow;
arrayIndex++;
teamDisplay.append(c.toString());
}
}
【问题讨论】:
-
也许你在这里被0除
average = attendence / no_of_matches ;? -
如果你得到 NaN,这意味着你将浮动零除以零。