Scanner
- There are two ways to receive strings: next and nextLine
public class Demo01 { public static void main(String[] args) { //Create a scanner object to receive keyboard data Scanner scanner0 = new Scanner(System.in); System.out.println("use next Reception mode:"); //Judge whether the user has entered a string if(scanner0.hasNext()){ //Receive in next mode String str =scanner0.next(); System.out.println("The output contents are:"+str); } Scanner scanner1 = new Scanner(System.in); System.out.println("use nextLine Reception mode:"); if(scanner1.hasNextLine()){ //Receive using nextLine String str1 =scanner1.nextLine(); System.out.println("The output contents are:"+str1); } //If the class belonging to IO stream is not closed, it will always occupy resources. Form the habit of closing it when it is used up scanner0.close(); scanner1.close(); } }
- next():
- Be sure to read valid characters before you can end the input
- The next() method will automatically remove the blank space encountered before entering valid characters
- Only after entering a valid character will the blank space after it be used as the separator or terminator
- next() cannot get a string with spaces
- nextLine():
- With Enter as the ending character, the nextLine() method returns all characters before entering carriage return
- Can get blank
Basic structure of Java
1. Sequential structure
- Between statements and between boxes, it is carried out in the order from top to bottom. It is composed of several processing steps executed in turn. It is a basic algorithm structure that any algorithm is inseparable from.
2. Select structure
-
if single selection structure
if(Boolean expression){ //If the Boolean expression is true, the statement is executed }
-
if double selection structure
if(Boolean expression){ //If the value of the Boolean expression is true }else{ //If the value of the Boolean expression is false }
-
Multiple choice structure
if(Boolean expression 1){ //If the value of Boolean expression 1 is true, execute the code }else if(Boolean expression 2){ //If the value of Boolean expression 2 is true, execute the code }else if(Boolean expression 3){ //If the value of Boolean expression 3 is true, execute the code }else{ //If none of the above Boolean expressions is true, execute the code }
-
Nested if structure
Nested if... else statements
if(Boolean expression 1){ //If the value of Boolean expression 1 is true, execute the code if(Boolean expression 2){ //If the value of Boolean expression 2 is true, execute the code } }
-
switch multiple selection structure
public static void main(String[] args) { char grade = 'c'; switch (grade){ case 'a': System.out.println("excellent"); break; case 'b': System.out.println("good"); break; case 'c': System.out.println("pass"); break; case 'd': System.out.println("fail,"); break; default: System.out.println("unknown"); } }
Case penetration phenomenon: when you do not use break to terminate, all statements of the sequential structure after the first case condition is met will be executed until the break statement is executed;
Default is the preset default value, which is executed when no matching case statement is executed;
Starting with javaSE 7, switch supports String types.
3. Circulation structure
-
while Loop
while(Boolean expression){ //Cyclic content }
-
As long as the Boolean expression is true, the loop will continue to execute.
-
In most cases, we will stop the loop. I also need a way to invalidate the expression to end the loop
-
In a few cases, the loop needs to be executed all the time, such as the request response listening of the server
-
If the loop condition is always true, it will cause wireless loop. We should try to avoid dead loop in normal business programming. It will affect the program performance or cause the program to get stuck and run away
-
-
do... while loop
do{ //Circular statement }while (Boolean expression)
- do... while execute first and then judge
- do... while loop statement will be executed at least once;
-
for loop
for(initialization;Boolean expression;to update){ //Code statement }
- for loop statement is a general structure that supports iteration. It is the most effective and flexible loop structure.
- The number of times the for loop is executed is actually determined before execution
-
Nested for loop, print 99 multiplication table and triangle
- multiplication table
public class ForDemo { public static void main(String[] args) { for(int j=1;j<=9;j++) { for(int i=1;i<=j;i++){//Print the contents of each line System.out.print(j+"*"+i+"="+(i*j)+"\t"); } System.out.println(); } } }
- Print triangles
public class TestDemo { public static void main(String[] args) { for(int i=1;i<=5;i++){ for(int j=5;j>=i;j--) System.out.print(" "); for (int j=1;j<=i;j++) System.out.print("*"); for (int j=1;j<i;j++) System.out.print("*"); System.out.println(); } } }
-
Enhanced for loop
for(Declaration statements: expressions){ //Code statement } public class ForDemo { public static void main(String[] args) { int[] arr={1,2,3,4,5}; for(int x:arr){ System.out.println(x); } } }
- Declaration statement: declare a new local variable. The type of the variable must match the type of the array element. Its scope is limited to the circular statement block, and its value is equal to the value of the secondary view array element
- Expression: an expression is the name of an array to access, or a method whose return value is an array
break and continue
-
break
- Break in the main part of any loop statement, you can use break to control the flow of the loop.
- break is used to forcibly exit the loop without executing the remaining statements in the loop
-
continue
- The continue statement is used in the loop statement body to terminate a loop process, that is, to skip the statements that have not been executed in the loop body, and then determine whether to execute the loop next time