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