给一个长度为7的整数型数组赋初值,并求它们的和及平均值,然后输出(用JAVA编写)
2个回答
展开全部
public class text
{
public static void main(String args[])
{
int[]x=new int[]{2,4,5,6,3,1,7};
int sum=0,average=0;
for(int i=0;i<x.length;i++)
sum+=x[i];
System.out.println("数组和为"+sum);
average=sum/x.length;
System.out.println("数组平均为"+average);
}}
…………………………………………………………………………………………
还有一种是需要构造函数的
class test
{
private int[] array={15,27,36,58,99,73,81};
private int n=array.length;
public void initialize()
{
}
public int getSum()
{
int sum=0;
for(int i=0;i<array.length;i++)
{
sum+=array[i];
}
return sum;
}
public int getAverage()
{
int average=0;
average=getSum()/array.length;
return average;
}
}
public class test2
{
public static void main(String args[])
{
initialize arr=new initialize();
System.out.println("该数组各元素之和为:"+arr.getSum());
System.out.println("该数组的平均值为:"+arr.getAverage());
}
}
{
public static void main(String args[])
{
int[]x=new int[]{2,4,5,6,3,1,7};
int sum=0,average=0;
for(int i=0;i<x.length;i++)
sum+=x[i];
System.out.println("数组和为"+sum);
average=sum/x.length;
System.out.println("数组平均为"+average);
}}
…………………………………………………………………………………………
还有一种是需要构造函数的
class test
{
private int[] array={15,27,36,58,99,73,81};
private int n=array.length;
public void initialize()
{
}
public int getSum()
{
int sum=0;
for(int i=0;i<array.length;i++)
{
sum+=array[i];
}
return sum;
}
public int getAverage()
{
int average=0;
average=getSum()/array.length;
return average;
}
}
public class test2
{
public static void main(String args[])
{
initialize arr=new initialize();
System.out.println("该数组各元素之和为:"+arr.getSum());
System.out.println("该数组的平均值为:"+arr.getAverage());
}
}
展开全部
简单一 个
public class Test{
public static void main(String[] args){
int[] aa = new int[7];
for(int i=0;i<aa.length;i++){
aa[i] = (int)(Math.random() *50);
}
int result = 0;
for(int i=0;i<aa.length;i++){
reslut += aa[i];
}
System.out.println("和:"+result);
System.out.println("平均:"+result/7.0);
}
}
public class Test{
public static void main(String[] args){
int[] aa = new int[7];
for(int i=0;i<aa.length;i++){
aa[i] = (int)(Math.random() *50);
}
int result = 0;
for(int i=0;i<aa.length;i++){
reslut += aa[i];
}
System.out.println("和:"+result);
System.out.println("平均:"+result/7.0);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询