Java random number

Source: Internet
Author: User

The code for random numbers in Javaz is

Math.random ().

I'm going to write a random number below:

Package Random;public class Random {public static void main (string[] args) {//TODO auto-generated method Stubint result= ( int) (Math.random () *100) +1;//This is the random number System.out.println between 1~100 (result);}}

Operation Result:

With random numbers, you can do some simple things, such as interacting with a small game:

Package Random;import Java.util.scanner;public class Random {public static void main (string[] args) {//TODO auto-generate D Method Stubscanner s=new Scanner (system.in); int result= (int) (Math.random () *100) +1; SYSTEM.OUT.PRINTLN ("Guess number than size"); System.out.println ("Please enter a number from 1 to 100 to see the same as our random number:"); while (true) {int guessnume=s.nextint (); if (Guessnume>result {System.out.println ("you guessed the number is big");} else if (Guessnume<result) {System.out.println ("the number you guessed is somewhat small");} Else{system.out.println ("Congratulations, you guessed it.") "); break;}}}

Operation Result:

Java random number

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.