Java Print random function

Source: Internet
Author: User

Tag: bre Static Next break number returns string int public

One, print 1-10 of the random function

public static void Randomprint () {
for (int i=0;i<100;i++) { //print 100 times
SYSTEM.OUT.PRINTLN ((int) (1+math.random () *10));
}
}

(int) (1+math.random () *10) int: Return type 1: Minimum 10 maximum value

Two, randomly output a number within the given range

Importpublicclass  randomnumber {       publicstatic   void  Main (string[] args) {            = {1,2,5,4,3,42,71,25,6};            New Random ();            int index = random.nextint (numbers.length);           System.out.println (Numbers[index]);}            }

Three, let the program run for a period of time to stop running (run 30 seconds)

 Public Static voidMain (string[] args) {LongBegain = System.currenttimemillis ();//Start System Time    Try{Thread.Sleep (100); } Catch(Exception e) {e.printstacktrace (); }    LongChecktime = System.currenttimemillis ();//Judging Time     while(true) {System.out.println (Checktime-begain); Try{Thread.Sleep (1000); } Catch(Exception e) {e.printstacktrace (); } checktime=System.currenttimemillis (); if((Checktime-begain) >= (30*1000)) {//judging time to 30 secondsSystem.out.println ("End after 30 seconds");  Break; }    }    }

After simplifying the code:

 Public Static voidMain (string[] args) {LongBegain = System.currenttimemillis ();//Start System Time    LongChecktime = System.currenttimemillis ();//Judging Time     while(true){        //Write the program you want to run here!!!!!
Checktime = System.currenttimemillis ();if((Checktime-begain) >= (30*1000)) {//judging time to 30 secondsSystem.out.println ("End after 30 seconds"); Break; } } }

Java Print random function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.