Java数组求平均值代码 java怎么求数组的平均值
用JAVA编写,数组求平均值问题!
看看这个吧,简单的方法
公司主营业务:成都网站建设、做网站、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联建站是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联建站推出三原免费做网站回馈大家。
import java.util.Arrays;
public class Test3 {
public static void main(String[] args) {
int[] score = {6,8,7,9,4,7,8,6,8,6};
Arrays.sort(score);//排序,排序后头尾都不要,这样就去掉了最高分和最低分
int sum = 0;
for(int i=1; iscore.length-1; i++) {//从第数组二个元素取到倒数第二个
sum += score[i];
}
double average = sum/8.0;
System.out.println("平均:" + average);
}
}
java输入数组,求平均值,求标准值!
以int类型数组为例,其他类型的都类似.
用户输入的时候用逗号隔开数字就可以了
import java.util.Scanner;
public class Baidu {
public static void main(String[] args) {
int[] ary = getArrayFromInput();
double average = calcArrayAverage(ary);
System.out.println("Average is: " + average);
int count = 0;
for (int item : ary) {
if (item average) {
count++;
}
}
System.out.println("Total " + count
+ " numbers greater than average of given array");
}
private static int[] getArrayFromInput() {
System.out.println("Please input some digits, separate with ',': ");
Scanner scanner = new Scanner(System.in);
String[] digits = scanner.nextLine().split(",");
int[] array = new int[digits.length];
for(int i = 0; i digits.length; i++){
array[i] = Integer.parseInt(digits[i].trim());
}
return array;
}
private static double calcArrayAverage(int[] ary) {
double arySum = 0D;
for (int item : ary) {
arySum += item;
}
return arySum / ary.length;
}
}
-----------
Please input some digits, separate with ',':
12, 35, 28, 127, 96, 35
Average is: 55.5
Total 2 numbers greater than average of given array
Java中编程求一个整数数组的最大值、最小值、平均值和所有数组元素的和
package
com.work20081209.main;
/**
*
Java中编程求一个整数数组的最大值、最小值、平均值和所有数组元素的和
*
*/
public
class
TestArray
{
private
int
maxValue;
private
int
minValue;
private
double
aveValue;
private
int
sumValue;
public
TestArray(int[]
nums){
maxValue
=
nums[0];
minValue
=
nums[0];
aveValue
=
nums[0];
sumValue
=
nums[0];
for
(int
i
=
1;
i
nums.length;
i++)
{
if(nums[i]
maxValue){
maxValue
=
nums[i];
}
if(nums[i]
minValue){
minValue
=
nums[i];
}
sumValue
=
sumValue
+
nums[i];
}
aveValue
=
sumValue/nums.length;
}
public
void
print(){
System.out.println("最大值:"+maxValue);
System.out.println("最小值:"+minValue);
System.out.println("平均值:"+aveValue);
System.out.println("和:"+sumValue);
}
public
static
void
main(String[]
args)
{
int[]
index
=
new
int[]{4,1,5,3,2};
TestArray
ta
=
new
TestArray(index);
ta.print();
}
}
JAVA求数组平均值
这样应该行
public class Test20
{
public static double[] a1(double[]...a)
{
double[] suma=new double[a.length];
for(int i=0;ia.length;i++){
double sum=0;
for(int j=0;ja[i].length;j++){
sum+=a[i][j];
if(j==(a[i].length-1)){
suma[i]=sum/a[i].length;
}
}
}
return suma;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
double a2;
double b2;
double c2;
double d2;
double a[]={90,85,66,51,69,78,88,94};
double b[]={68,88,81,90,65,74,64,46};
double c[]={68,90,85,88,68,70};
double d[]={60};
double[] avg=a1(a,b,c,d);
a2=avg[0];
b2=avg[1];
c2=avg[2];
d2=avg[3];
System.out.println(a2+","+b2+","+c2+","+d2);
}
}
当前题目:Java数组求平均值代码 java怎么求数组的平均值
文章源于:http://azwzsj.com/article/docogej.html