public class SumAndAverage {
public static void main(String[] args) {
// TODO Auto-generated method stub
int sum = 0;
int num;
for(num = 0; num <= 100; num++){
sum += num;
}
double average = ((double)sum) / num;
System.out.println("The sum is " + sum);
System.out.println("The average is " + average);
}
}
结果:
总和为 5050 平均值为 50.0
怎么了:
5050 / 100 = 50.5 不是 50.0