第1个回答 2009-05-20
public class random1 {
public static void main(String[] args) {
Random random=new Random();
int[] a=new int[10];
int sum=0;
for (int i = 0; i < a.length; i++) {
a[i]=random.nextInt(100);//随机产生0-100的随机数 要变动请自行更改
sum=sum+a[i];
System.out.println(a[i]);
}
System.out.println("avg:"+sum/a.length);//取整 是否要小数请自行更改
}
}本回答被提问者采纳
第2个回答 2009-05-20
public class TestArray {
public static void main(String[] args) {
int a[] = new int[10];
for(int i = 0 ;i<10;i++) {
int num = (int)(Math.random()*100);//0到100的范围的Math.random()*99+1 //1到100的
a[i]=num;
System.out.println(num);
}
int sum=0;
for (int i = 0; i < a.length; i++) {
sum = sum+a[i];
}
System.out.println("平均值为"+sum/10);
}
}
第3个回答 2009-05-20
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Random rand = new Random(System.currentTimeMillis());
int[] arr = new int[10];
int sum = 0;
for (int i = 0; i < 10; i++)
{
arr[i] = rand.nextInt();
sum += arr[i];
}
System.out.println("平均值为:" + sum / 10);
}
}
第4个回答 2009-05-20
import java.util.Random;
public class RandomAverage{
public static void main(String[] args){
int[] a=new int[10];
int sum;
Random r=new Random();
for(int i=0;i<a.length;i++){
a[i]=r.nextInt();
sum+=a[i];
}
double ave=(double)sum/a.length;
for(int i=0;i<a.length;i++)
System.out.println(a[i]+" ");
System.out.println("The average is "+ave);
}
}