ranutilities.java

来自「servletjsp一些应用程序」· Java 代码 · 共 37 行

JAVA
37
字号
package coreservlets; // Always use packages!!

/** Simple utility to generate random integers.
 *  <P>
 *  Taken from Core Servlets and JavaServer Pages 2nd Edition
 *  from Prentice Hall and Sun Microsystems Press,
 *  http://www.coreservlets.com/.
 *  &copy; 2003 Marty Hall; may be freely used or adapted.
 */

public class RanUtilities {
  
  /** A random int from 1 to range (inclusive). */

  public static int randomInt(int range) {
    return(1 + ((int)(Math.random() * range)));
  }

  /** Test routine. Invoke from the command line with
   *  the desired range. Will print 100 values.
   *  Verify that you see values from 1 to range (inclusive)
   *  and no values outside of that interval.
   */
  
  public static void main(String[] args) {
    int range = 10;
    try {
      range = Integer.parseInt(args[0]);
    } catch(Exception e) { // Array index or number format
      // Do nothing: range already has default value.
    }
    for(int i=0; i<100; i++) {
      System.out.println(randomInt(range));
    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?