Random and Scanner in Java and their looping statements

Source: Internet
Author: User

1.1. function and usage of scanner class and Random class

Scanner is the default to read numbers by line. Create a function to enter
First Step: Guide package: Import Java.util.Scanner; Step two: Scanner scan=new Scanner (System. inch );//system.in is a third step from the console: Then receive this value with a variable class
int n=scan.nextint ();

          common methods :public int nextint () : Get keyboard input data, return to int type

                     public String Next () : Get keyboard input data, return string

  
2.Random method is a pseudo-randomly generated number method
Span style= "font-size:15px" > First step: Guide package: Import Java.util.Scanner; Step two: definition: Random scan = new Random ();
step three: int N=scan.nextint (); //If there are values in parentheses, such as: 10, randomly generate 0 to 90 numbers.

Public Double nextdouble () a random decimal that produces a range of [0,1] , contains 0.0, and does not contain 1.0.



Note: The number placed is zero to number, but does not contain the number itself .

There are 3 formats for 2.if statements

    1 . if  ... else  if      ...else statement is used to judge multiple conditions and handle many different kinds of processing.   
if (conditional expression 1) {
Execute code 1;
}else if ( conditional expression 2 ) {
execute code 2;
}else{
execute code 3; span>
}

Execution Process: Which conditional expression is set up, executes the corresponding code,

if all the conditional expressions are not true, the body of the statement in the last else is executed


2. if The. Else statement means that if a certain condition is met, some sort of processing is performed, or another treatment is performed.

          if (conditional expression) {
Execution Code 1;
}else{
Execution Code 2;
}

If the conditional expression value is true, execute code 1

           If the conditional expression value is fales, execute code 2

3. The If statement refers to some kind of processing if a certain condition is met.
if (conditional expression) {
Execute code;
}

Execution Process: If the conditional expression value is true, the code is executed

                    If the conditional expression value is false, the code is not executed


3.for Loop, while loop, Do.....while loop

 1.
for(Condition: ①;②;③) {

Loop statement:④;
The order of execution is:①==>②==>④==>③


2.
while(condition) {

Loop statement;
Thewhile loop indicates that it is executed first, and will not be executed once if the loop condition is not satisfied.

3. Do{

Loop statement;

} while(conditions);

Do.. The while loop indicates that it is executed first and then, if the loop condition is not satisfied , at least once.

add: The For loop applies to the number of times the loop is determined

While loops anddo. . While loop number of uncertainties

Note: The semicolon after the closing sentence;


4. Jump Control Statements

Break ; Jump out of the loop without performing the following loop

continue; To end this loop, proceed to the following loop

Random and Scanner in Java and their looping statements

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.