I am trying to work out the average of the array but I cannot convert the int into a double? why is this happening?
private void AvgOfArray(){
myWindow.clearOut();
int total = 0;
int[] a = new int[4];
int i = 0;
double avg = 0.0;
while (i < a.length) {
a[i] = 1 + (int) (Math.random() * 10);
myWindow.writeOutLine(a[i]);
total += a[i];
i++;
}
avg = (Double) i;
myWindow.writeOutLine(total/Double.parseDouble(i));
}
The correct way to calculate the average is something like,
double avg = total / (double) i;
myWindow.writeOutLine(avg);
Boxing an int
to a Double
doesn't make much sense, especially to then try and parse it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.