(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]+"	");   
        }
      }  
}    

 输出结果:

 

原文地址:https://www.cnblogs.com/william-dai/p/9187073.html