java平均的随机数_java已知平均数,生成几个随机数的平均数为指定的平均数

展开全部

import java.util.Random;

public class Test{

/**

* 计算平均数

* @param numSize 随机的数字个数

* @param average 平均值

* @return 随机数数组

*/

public int[] test(int numSize,int average)

{

Random rand = new Random();

int result[] = new int[numSize];

if(numSize%2 ==0)

{

for(int i = 0;i

{

int rand1 = rand.nextInt();

int leave1 = average - rand1;

int leave2 = average + rand1;

result[i] = leave1;

result[i+1] = leave2;

}

}

else

{

for(int i = 0;i

{

int rand1 = rand.nextInt();

int leave1 = average - rand1;

int leave2 = average + rand1;

result[i] = leave1;

result[i+1] = leave2;

}

result[numSize-1] = average;

}

return result;

}

public static void main (String[] args){

int test[] = new Test().test(8, 16);

int total = 0;

for(int i =0;i

{

System.out.println("第"+(i+1)+"个数:"+test[i]);

total +=test[i];

}

System.out.println("总和e5a48de588b662616964757a686964616f31333335336433:"+total);

}

}

e33fee7d22973cfa4036e6f8ff7881c2.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值