java随机生成6位正整数_随机正整数生成类的JAVA实现

随机正整数生成类

/**

*Name:CreateRandomNumber.java

*Author:Richard

*Date:Apr-10,2006

*Version:1.0.0.0

*============================

*随机整数生成类

*============================

*Author  Version   Date   Changes

*Richard  1.0.0.2   Apr-11,2006  use EXCEPTION to instead of "if"

*/

public class CreateRandomNumber{

protected int r_min;

protected int r_max;

protected int r_num;

protected int r_arr[];

protected int r_mid;

/**

*construct method

*/

public CreateRandomNumber(){

r_min = 0;

r_max = 0;

r_num = 0;

r_arr = new int[r_num];

r_mid = 0;

}

/**

*set method

*/

public CreateRandomNumber(int pr_min, int pr_max, int pr_num, int pr_mid){

try{

if(pr_num<0)

throw new CreateRandomArrayException("ERROR: Num is less than zero!");

else if(pr_min>pr_max)

throw new CreateRandomArrayException("ERROR: min is greater than max!");

else if(pr_midpr_max)

throw new CreateRandomArrayException("ERROR: mid is not between min and max!");

else{

r_min = pr_min;

r_max = pr_max;

r_num = pr_num;

r_mid = pr_mid;

r_arr = new int[r_num];

}

}

catch(CreateRandomArrayException e){

System.out.println(e.getMessage());

}

}

/**

*create numbers between r_min and r_max as r_num setting

*/

public void createRandom(){

for(int i=0; i

int t = r_max-r_min;

r_arr[i] = r_min+(int)(t*Math.random());

}

}

/**

*print numbers

*/

public void printArr(){

System.out.println("===========The result===========");

for(int i=0; i

System.out.println("arr[" +i+ "]=" +r_arr[i]);

}

/**

*print the max and min result

*/

public void maxAndMin(){

int max = r_arr[0];

int min = r_arr[0];

for(int i=1; i

if(r_arr[i]>max)

max = r_arr[i];

if(r_arr[i]

min = r_arr[i];

}

System.out.println("The max is :" +max);

System.out.println("The min is :" +min);

}

/**

*count how many numbers are greater than r_mid

*/

public int countNum(){

int count = 0;

for(int i=0; i

if(r_arr[i]>r_mid) count++;

return count;

}

/** *count how many numbers are greater than pr_mid */    public int countNumEqualsrPr_mid(int pr_mid){       int count = 0;  for(int i=0; i

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值