zoukankan      html  css  js  c++  java
  • (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]+"	");   
            }
          }  
    }    

     输出结果:

     

  • 相关阅读:
    2020春Contest
    HDU Count the string (KMP)
    P1757 通天之分组背包
    L1-050 倒数第N个字符串
    3月份目标
    Division UVa725
    数三角
    luogu P2051 [AHOI2009]中国象棋 dp 状态压缩+容斥
    Codeforces Round #654 (Div. 2) E
    Codeforces Round #654 (Div. 2) D
  • 原文地址:https://www.cnblogs.com/william-dai/p/9187073.html
Copyright © 2011-2022 走看看