User Interaction Scanner

Scanner s = new Scanner(System.in); 
Copy the code

next & nextLine

Public static void main(String[] args) {// Create a Scanner object that receives keyboard data Scanner Scanner = new Scanner(system.in); // Next receives string system.out.println ("Next receives :"); If (scanner. HasNext ()){String STR = scanner. Next(); System.out.println(" input: "+ STR); }// Close the IO stream scanner.close(); }Copy the code

Test data: Hello World!

Result: Only Hello is printed.

public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("nextLine: "); If (scans.hasNextline ()) {String str2 = scans.nextline (); System.out.println(" input: "+ str2); }scan.close(); }Copy the code

Test data: Hello World!

The result: Hello World!

 next():

  1. You must read a valid character before you finish typing.
  2. The next() method automatically removes whitespace encountered before entering valid characters.
  3. Only after a valid character is entered, the whitespace following it is used as a delimiter or terminator.
  4. Next () does not get a string with Spaces.

NextLine () :

  1. The nextLine() method returns all characters up to Enter.
  2. You can get whitespace.

Sequential structure

The basic structure of JAVA is a sequential structure that executes sequentially, sentence by sentence, unless specified otherwise.

The sequential structure is the simplest algorithm structure.

It is composed of several processing steps executed sequentially. It is a basic algorithm structure that any algorithm can not do without.

Sequence structure is embodied in program flow chart by connecting program frames from top to bottom with flow line to execute algorithm steps sequentially.

public static void main(String[] args) { System.out.println("Hello1"); System.out.println("Hello2"); System.out.println("Hello3"); System.out.println("Hello4"); System.out.println("Hello5"); }// Execute from top to bottom! Output in sequence.Copy the code

Choose structure

public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Print (" Please input: "); String s = scanner.nextLine(); If (s.quals ("Hello")){system.out.println (" input: "+s); }System.out.println("End"); scanner.close(); }Copy the code

When using if, else if, else statements, note the following:

  • An if statement can have at most one else statement, which follows all else if statements.
  • An if statement can have several else if statements, which must precede the else statement.
  • Once one of the else if statements checks true, the other else if and else statements are skipped.

Switch Multi-select structure

Switch (expression){case value: // statement break; // Case value: // statement break; You can have any number of case statements default: // Optional // statements}Copy the code

The switch case statement has the following rules:

  • Variable types in switch statements can be byte, short, int, or char. As of Java SE 7, the Switch supports strings, and case tags must be String constants or literals.
  • Switch statements can have multiple case statements. Each case is followed by a value to be compared and a colon.
  • The data type of a value in a case statement must be the same as the data type of a variable, and must be either constant or literal.
  • When the value of the variable is equal to the value of the case statement, the statement following the case statement is executed, and the switch statement is not broken until the break statement occurs.
  • The switch statement terminates when a break statement is encountered. The program jumps to the statement following the switch statement. Case statements need not contain a break statement. If no break statement occurs, the program continues to execute the next case statement until a break statement occurs.
  • Switch statements can contain a default branch, which is typically the last branch of the switch statement (anywhere, but last is recommended). Default is executed when no case statement value is equal to the variable value. The default branch does not require a break statement.

When the switch case is executed, it must first match the value of the current case, and then judge whether to continue output or jump judgment according to whether there is a break.

If there is no break statement in the case statement block, the values of all subsequent cases are printed starting from the current case. If a subsequent case block has a break statement, the judgment is broken. 【 Case through 】

The while loop

While (Boolean expression) {// loop content}Copy the code

As long as the Boolean expression is true, the loop continues.

public static void main(String[] args) { int i = 0; While (I <100){I ++; System.out.println(i); }} public static void main(String[] args) {while (true){// Waiting for client connection // timed check //...... }}Copy the code

A loop condition that is always true creates an infinite loop, which should be avoided in normal business programming. Will affect the program performance or cause the program stuck crash!

The do… The while loop

For a while statement, if the condition is not met, the loop cannot be entered. But sometimes we need to do it at least once, even if the conditions are not met.

The do… The while loop is similar to the while loop, except that do… The while loop is executed at least once.

Do {// code statement}while(Boolean expression);Copy the code

The difference between while and do-while:

  1. While determines and then executes. Dowhile is execute and judge later
  2. do… The while guarantees that the loop will be executed at least once

The for loop

The for loop statement is a generic structure that supports iteration and is the most efficient and flexible loop structure.

The number of times the for loop is executed is determined prior to execution. The syntax is as follows:

For (initialize; Boolean expression; Update) {// code statement}Copy the code
  • The initialization step is performed first. A type can be declared, but one or more loop control variables can be initialized, or it can be an empty statement.

  • Then, the value of the Boolean expression is tested. If true, the loop body is executed. If false, the loop terminates and the statement following the body of the loop begins execution.

  • After a loop is executed, the loop control variables are updated (iteration factors control how much the loop variable increases or decreases).

  • Check the Boolean expression again. Repeat the above procedure.

    public static void main(String[] args) { int a = 1; // initialize while(a<=100){system.out.println (a); // a+=2; } system.out. println(” While loop ends! “) ); for(int i = 1; i<=100; I ++){// initialize // conditional judgment // iteration system.out.println (I); // loop body} system.out. println(” While loop ends! ); }

Enhanced for loop

For (declaration statement: expression) {// code sentence}Copy the code

Declaration statement: Declares a new local variable whose type must match that of the array element. Its scope is limited to the loop block, and its value is equal to the value of the array element at that time.

Expression: An expression is the name of the array to access, or a method that returns an array.

public static void main(String[] args) { int [] numbers = {10, 20, 30, 40, 50}; for(int x : numbers ){ System.out.print( x ); System.out.print(","); }System.out.print("\n"); String [] names ={"James", "Larry", "Tom", "Lacy"}; for( String name : names ) { System.out.print( name ); System.out.print(","); }}Copy the code

Break the keyword

Break is used mainly in loop or switch statements to break out of the entire statement block.

Break breaks out of the innermost loop and continues to execute the statements below the loop.

public static void main(String[] args) { int i=0; while (i<100){ i++; System.out.println(i); if (i==30){ break; }}}Copy the code

The continue keyword

Continue applies to any loop control structure. The effect is to immediately jump to the next iteration of the loop.

In the for loop, the continue statement causes the program to jump immediately to the update statement.

While or do… In the while loop, the program immediately jumps to the Boolean expression statement.

public static void main(String[] args) { int i=0; while (i<100){ i++; if (i%10==0){ System.out.println(); continue; }System.out.print(i); }}Copy the code

In the body of any loop statement, a break can be used to control the flow of the loop. Break is used to force out of the loop without executing the remaining statements in the loop. (The break statement is also used in switch statements)

The continue statement is used in the body of a loop to terminate a loop by skipping statements in the body that have not yet been executed and then deciding whether to execute the loop next time.