Tenth week (11.18-11.24)----Personal project----Learning Java Summary 2

Source: Internet
Author: User

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

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.