Sequential structure
◆ the basic structure of JAVA is sequential structure. Unless otherwise specified, it will be executed sentence by sentence in sequence.
◆ sequential structure is the simplest algorithm 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 cannot be separated from.
package com.kuang.struct; public class ShunXuDemo { public static void main(String[] args) { //Sequential structure System.out.println("hello1"); System.out.println("hello2"); System.out.println("hello3"); System.out.println("hello4"); System.out.println("hello5"); /* result: hello1 hello2 hello3 hello4 hello5 */ } }
Select structure
if single selection structure
◆ we often need to judge whether something is feasible before we execute it. Such a process is represented by if statement in the program.
◆ syntax:
package com.kuang.struct; import java.util.Scanner; public class ifDemo01 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter:"); String s = scanner.nextLine(); //equals: determines whether the strings are equal if (s.equals("Hello")){ System.out.println(s); } System.out.println("End"); scanner.close(); /* result: Please enter: Hello Hello End result: Please enter: ac End */ } }
if double selection structure
◆ now there is a demand. The company wants to buy a software. If it succeeds, it will pay 1 million yuan to others. If it fails, it will find someone to develop it by itself. We need to have two judgments and a double choice structure, so we have an if else structure.
◆ syntax:
package com.kuang.struct; import java.util.Scanner; public class ifDemo02 { public static void main(String[] args) { //If the test score is more than 60, you will pass, and if it is less than 60, you will fail. Scanner scanner = new Scanner(System.in); System.out.println("Please enter your grade:"); int score = scanner.nextInt(); if (score>60){ System.out.println("pass"); }else { System.out.println("fail,"); } scanner.close(); } }
if multiple selection structure
◆ we found that the code just now does not conform to the actual situation. In the real situation, there may be ABCD and interval multi-level judgment. For example, 90-100 is A and 80-90 is B Wait, in life, we often have more than two choices, so we need A multi-choice structure to deal with such problems!
◆ syntax:
package com.kuang.struct; import java.util.Scanner; public class ifDemo03 { public static void main(String[] args) { //If the test score is more than 60, you will pass, and if it is less than 60, you will fail. Scanner scanner = new Scanner(System.in); /* if The statement can have at most one else statement, which is after all else if statements. if A statement can have several else if statements that sit before the else statement. Once one else if statement is detected as true, its else if and else statements will skip execution. */ System.out.println("Please enter your grade:"); int score = scanner.nextInt(); if (score==100){ System.out.println("Congratulations, full marks"); }else if (score<100 && score>=90){ System.out.println("A level"); }else if (score<90 && score>=80){ System.out.println("B level"); }else if (score<80 && score>=70){ System.out.println("C level"); }else if (score<70 && score>=60) { System.out.println("D level"); }else if (score<60 && score>=0) { System.out.println("fail,"); }else { System.out.println("Illegal results"); } scanner.close(); } }
Nested if structure
◆ use nested if Else statement is legal. That is, you can use an if or else if statement in another if or else if statement. You can nest else if like an IF statement else.
◆ syntax:
◆ thinking? We need to find a number between 1 and 100
It can be found in 1-50, 50-100, and so on
switch multiple selection structure
◆ another implementation method of multi selection structure is switch case statement.
◆ switch case statement judges whether a variable is equal to a value in a series of values. Each value is called a branch.
◆ the types of variables in the switch statement can be:
-
byte, short, int or char
-
Start with Java SE 7
-
switch supports String type
-
At the same time, the case tag must be a string constant or literal.
package com.kuang.struct; public class SwitchDemo01 { public static void main(String[] args) { //case penetration / / switch matches a specific value char grade = 'C'; switch (grade){ case 'A': System.out.println("excellent"); break;//Optional case 'B': System.out.println("good"); break;//Optional case 'C': System.out.println("pass"); break;//Optional case 'D': System.out.println("make persistent efforts"); break;//Optional case 'E': System.out.println("fail"); break;//Optional default: System.out.println("Unknown level"); } } }
package com.kuang.struct; public class SwitchDemo02 { public static void main(String[] args) { String name = "Mad God"; //JDK7's new feature, the expression result can be a string!!! //The essence of characters is still numbers //Decompile Java -- class (bytecode file) -- decompile (IDEA) //Right click the directory path in the struct package, open the folder, and drag the class file selected in out to the struct target folder switch (name){ case "Qin Jiang": System.out.println("Qin Jiang"); case "Mad God": System.out.println("Mad God"); default: System.out.println("What are you doing!"); } /* result: Mad God What are you doing! */ } }
Decompile bytecode file
name.hashCode
Each object has its own hashCode, which is generated by a certain algorithm. In the class file, the hash value is used to determine whether it is equal to the string