用JAVA定义长度为5的float型数组,输入5个数到数组中 计算出该数组所有元素的平均值

跪求正解

public class Test {
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static String getString(){
try {
return br.readLine();
} catch (IOException e) {
return "";
}
}
public static int getInt(){
return Integer.parseInt(getString());
}

public static float getFloat(){
return Float.parseFloat(getString());
}


public static void main(String[] args){
float[] s = new float[5];
for(int i=0; i<s.length; i++){
System.out.print("请输入第 " + (i+1) + " 个数: ");
s[i] = getFloat();
}

float sum = 0;
for(int i=0; i<s.length; i++){
sum += s[i];
}

System.out.println("平均数: " + (sum/s.length));

}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-09-13
这个很简单呀...
这个就是平均值=sum / sum.length;
相似回答