(PASS)java中nextInt()函数


一:获取随机数的函数:

package
test; import java.util.Random; /** * * @ClassName: NextIntDemo * @Description: nextInt()函数 * @author William_Dai * @date 2019年5月21日 * */ public class NextIntDemo { public static void main(String[] args) { //如果想得到30到200的(包含30和200)这个跨度的数在java中一般可以有如下方式获得 //方法一: //int i = (int)(Math.random()*2) + 30; //[30,31] int i = (int)(Math.random()*171) + 30; [30,200] //方法二: Random r = new Random () ; //int j = r.nextInt (2) ; // [0,1] //int j = r.nextInt (201) ; // [0,200] int j = r.nextInt (171) + 30; // [30,200] //System.out.println("j:"+j); } }

 

 二:构造一个数组并打印

package test;

import java.util.Random;

/**
 * 
 * @ClassName: NextIntDemo
 * @Description: nextInt()函数
 * @author William_Dai
 * @date 2019年5月21日
 *
 */
public class NextIntDemo {
     public static void main(String args[]){  
             int[] array=creatArray(10);  //构造数组 
                printArray(array); //以固定格式打印数组  
         }  
     
     public static int[] creatArray(int length){  //构造含length个元素的数组的方法  
          int[] array =new int[length];   
          Random rad=new Random();   //产生随机数的方法  
          for(int i=0;i<array.length;i++){   
              int value = rad.nextInt(100); //rad.nextInt(100) 意思是随机产生一个大于等于0小于100的数  --即包含0不包含100  
              array[i]=value;   
           }   
         return array;   
      }   
       
     public static void printArray(int[] array){  //固定格式打印数组 
        for(int i=0;i<array.length;i++){ 
            System.out.print(array[i]+"\t");   
        }
      }  
}    

 输出结果:

 

 

转载于:https://www.cnblogs.com/william-dai/p/9187073.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值