Java Basic Learning Notes--6 (Scanner, Random)

Source: Internet
Author: User
Tags sca

1. Scanner class

the console scan class provided by the JDK is used to read information from the console input. Import Java.util.Scanner required before use; (Import Package).

Use:

Created object: Scanner SCA = new Scanner (system.in);

Get string: String str1 = Sca.next (); The end can be indicated by a space, carriage return line character (\ r \ n)

String str2 = Sca.nextline (); can only end with \ r \ n

Get integer: int num = Sca.nextint ();

Get decimal: Double d = sca.nextdouble ();

Case 8:

650) this.width=650; "style=" width:661px;height:413px; "title=" 8b3q%p85f) 2s3.png "src=" http://s3.51cto.com/wyfs02/ M01/6c/c1/wkiom1vrdtdtt5z0aajpobtw1t4630.jpg "width=" 721 "height=" 457 "alt=" wkiom1vrdtdtt5z0aajpobtw1t4630.jpg "/ >

Case 9:

650) this.width=650; "style=" width:660px;height:405px; "title=" 9e.png "src=" http://s3.51cto.com/wyfs02/M0 1/6c/bc/wkiol1vrduedrac2aakhtqks-wc236.jpg "width=" 740 "height=" 437 "alt=" wkiol1vrduedrac2aakhtqks-wc236.jpg "/ >

Output Result:

650) this.width=650; "title=" 999aj4qy9_@) drf.png "src=" http://s3.51cto.com/wyfs02/M01/6C/C1/wKiom1VRdY2y Zo56aacudvi8n2q231.jpg "alt=" Wkiom1vrdy2yzo56aacudvi8n2q231.jpg "/>

2. Random class

The JDK provides a class that obtains random numbers, which are used to obtain random decimals, integers, Boolean types, and so on. Import java.util.Random required before use;

Use:

Create object: Random random = new random ();

     / /Get a random integer in the range: int num = Ran.nextint (n);//value [0, N)

Gets the random Boolean value: Boolean flag = Ran.nextboolean ();

Get random decimals: double d = ran.nextdouble ();

Case Study:

650) this.width=650; "title=" 10.png "src=" Http://s3.51cto.com/wyfs02/M02/6C/C1/wKiom1VRdevSgt-JAAHX9Pliv Oi537.jpg "alt=" Wkiom1vrdevsgt-jaahx9plivoi537.jpg "/>

This article is from "Forever Young" blog, please be sure to keep this source http://kingkongzhao.blog.51cto.com/6319491/1650551

Java Basic Learning Notes--6 (Scanner, Random)

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.