First, get the random number
Method 1 (data type) (minimum value +math.random () * (max-min. +1)), note that each parenthesis here is best not omitted.
Cases:
1 Public Static voidMain (string[] args) {2 //TODO auto-generated Method Stub3 for(inti = 0;i<30;i++){4 intSuijishu = (int) (1+math.random () * (10-1+1));//gets the random number from 1 to 10 and converts it to an integer type5System.out.print (suijishu+ ";"));6 }7}
Operation Result:
Method 2 obtains the random number by java.util the Nextxxx () method of the random class in the package. The Nextint (k) method can return the random integer number of the 0-k , and the results are as follows.
Cases:
1 Public Static voidMain (string[] args) {2 //TODO auto-generated Method Stub3Random r =NewRandom ();4 for(inti = 0;i<30;i++)5 {6System.out.print (R.nextint (4) + ";");7 }8}
Results:
Nextdouble () gets a random double number, which is obtained by nextdouble () * (b-a) +a to get the double random number of the interval in [A, b] .
Cases:
1 Public Static voidMain (string[] args) {2 //TODO auto-generated Method Stub3Random r =NewRandom ();4 DoubleA = 0.0,b = 0.2;//interval in [0.0,0.2]5 6 for(inti = 0;i<30;i++)7 {8System.out.println (R.nextdouble () * (b-a) +a+ ";);9 }Ten}
Results:
Second, integer conversion to String type
Method 1 valueof method for string
Cases:
1 Public Static void Main (string[] args) {2 // TODO auto-generated Method Stub 3 int i=5; 4 String s=string.valueof (i); 5 System.out.println (s); 6 7 }
Results:
Method two adds an empty string directly after the int
Cases:
1 Public classSuijishu {2 3 Public Static voidMain (string[] args) {4 //TODO auto-generated Method Stub5 intI=5;6String s = i+ "";7 System.out.println (s);8 9 }Ten One}
Results:
Method Three: Use int's wrapper class integer, and use his tostring method in the integer.
Cases:
1 Public Static void Main (string[] args) {2 // TODO auto-generated Method Stub 3 int i=5; 4 String s = integer.tostring (i);; 5 System.out.println (s); 6 7 }
Results:
Tenth week (11.18-11.24)----Personal project----Learning Java Summary 2