JAVA random number,

Source: Internet
Author: User

JAVA random number,

In JAVAZ, the random number code is

Math. random ().

Next I will write a random number:

Package random; public class random {public static void main (String [] args) {// TODO Auto-generated method stubint result = (int) (Math. random () * 100) + 1; // This is 1 ~ Random Number between 100: System. out. println (result );}}

Running result:

Through the random number, you can also do some simple operations, such as a small game Interaction:

Package random; import java. util. usage; public class random {public static void main (String [] args) {// TODO Auto-generated method stubScanner s = new usage (System. in); int result = (int) (Math. random () * 100) + 1; System. out. println ("guess the digit ratio"); System. out. println ("enter a number from 1 to 100 to see if it is the same as our random number:"); while (true) {int guessnume = s. nextInt (); if (guessnume> result) {System. out. println ("the number you guessed is too large");} else if (guessnume <result ) {System. out. println ("the number you guessed is a little small");} else {System. out. println ("Congratulations, you guessed it. "); Break ;}}}}

Running result:

 

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.