xxxxxxxxxx
Just some minor modification to your code will do (with some var renaming for clarity) :
double sum = 0; //average will have decimal point
for(int i=0; i < args.length; i++){
//parse string to double, note that this might fail if you encounter a non-numeric string
//Note that we could also do Integer.valueOf( args[i] ) but this is more flexible
sum += Double.valueOf( args[i] );
}
double average = sum/args.length;
System.out.println(average );
xxxxxxxxxx
/**
* Java Program - Average of Numbers
*/
public class Average {
public static void main(String[] args) {
//numbers
int[] nums = {1, 2, 3, 4, 5, 6};
float sum = 0;
//compute sum
int i=0;
while(i < nums.length) {
sum += nums[i];
i++;
}
//compute average
float average = (sum / nums.length);
System.out.println("Average : "+average);
}
}