编写一个java Application,输出一个整数数组中最大和最小的元素、平均值以及所有元素的和。

详细点的... 详细点的 展开
 我来答
wangjia5462873
2009-09-09 · TA获得超过107个赞
知道答主
回答量:39
采纳率:0%
帮助的人:0
展开全部
import java.util.Arrays;

public class Array{
public static void main(String[] args)
{
int a[]=new int []{31,26,93,44,65};
Arrays.sort(a);
System.out.println("min:"+a[0]);
System.out.println("max:"+a[a.length-1]);

int count=0,average;
for(int i = 0;i<a.length;i++)
count +=a[i];
average = count/i.length;

System.out.println("count:"+count);
System.out.println("average :"+average);
}
}

这是代码量最小的解决方案。
其中的关键是Arrays.sort(a);
静态类Arrays提供静态方法sort():用来对array进行从小到大的排序。

君子性非异也,善假于物也。
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2016-08-29
展开全部
public class BBBB {
public static int max(int[] a)
{
int temp = a[0];
for(int i=0;i<a.length;i++)
{
temp = java.lang.Math.max(a[i],temp);
}
return temp;
}
public static int min(int[] a)
{
int temp = a[0];
for(int i=0;i<a.length;i++)
{
temp = java.lang.Math.min(a[i],temp);
}
return temp;
}
public static float avg(int[] a)
{
int temp = 0;
for(int i=0;i<a.length;i++)
{
temp = a[i]+temp;
}
return (float)temp/a.length;
}
public static int sum(int[] a)
{
int temp = 0;
for(int i=0;i<a.length;i++)
{
temp = a[i]+temp;
}
return temp;
}
public static void main(String[] args)
{
int[] a = new int[]{1,2,3,4,5,6,7,8};
System.out.println(max(a));//最大值
System.out.println(min(a));//最小值
System.out.println(avg(a));//平均值
System.out.println(sum(a));//和
}

}
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
非情林q
2009-09-09 · TA获得超过987个赞
知道小有建树答主
回答量:1157
采纳率:100%
帮助的人:959万
展开全部
public class ArrayTest {

/**
* @param args
*/
public static void main(String[] args) {

int[] array = new int[]{1, 3, 4, 8, 19};

int minValue= array[0];
int maxValue = array[0];
int aveValue = array[0];
int countValue = array[0];

for (int i = 1; i < array.length; i++) {
if (minValue > array[i]) {
minValue = array[i];
}

if (maxValue < array[i]) {
maxValue = array[i];
}

countValue = countValue + array[i];
}

aveValue = countValue/array.length + countValue%array.length;

System.out.println("minValue:" + minValue);
System.out.println("maxValue:" + maxValue);
System.out.println("aveValue:" + aveValue);
System.out.println("countValue:" + countValue);
}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
杰比·优德尔
2009-09-09 · TA获得超过243个赞
知道答主
回答量:47
采纳率:0%
帮助的人:0
展开全部
直接给你答案例子:
public class Array {

public static void main(String args[]) {
int[] a={2,5,12,45,89,45,36,95,458,6534,15,39};
int max=0;
int min=10000;
int count=0;
float aver=0;
for(int i=0;i<a.length;i++) {
if(max<a[i])
max=a[i];
if(min>a[i])
min=a[i];
count=count+a[i];
}
aver=count/a.length;
System.out.println("最小值:"+min+"\n"+"最大值:"+max+"\n"+"和值:"+count+"\n"+"平均值:"+aver);
}
}
这详细吧?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
解燎3e
2009-09-09 · TA获得超过206个赞
知道小有建树答主
回答量:452
采纳率:0%
帮助的人:324万
展开全部
用for循环,遍历整个数组,很容易得到你想要的
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 2条折叠回答
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式