我试图找到数字的数组的平均值,其值取自JTextfield
。我的问题是零与平均值计算在一起。如何找到平均值但不包含零。整数不为0的整数的平均值作为数
int[] numbers = new int[]{Integer.parseInt(math1.getText()),0 };
int sum =0;
if(numbers != 0){
for(int i=0; i < numbers.length ; i++)
sum = sum + numbers[i];
//calculate average value
int average = sum/numbers.length;
totdata2.setText(Integer.toString(average));
}
0的重要性..如果有人给你0/10,你想包括它! – christopher 2015-04-02 20:59:54
使用“if”使您的代码有条件。 – 2015-04-02 21:07:59