catalog:
1. User interaction Scanner
2. Sequential structure
3. Select structure
4. Cyclic structure
5,break & continue
6. Practice
2, Sequential structure
The basic structure of JAVA is sequential structure. Unless otherwise specified, it will be executed in sequence sentence by sentence.
Sequence structure is the simplest algorithm structure.
======>From top to bottom
Between sentences and between boxes, the order is from top to bottom
It is composed of several processing steps which are executed in turn. It is a basic algorithm structure that any algorithm cannot do without.
3, Select structure
&3.1 if single selection structure
&3.2 if double selection structure
&3
&3.4 nested if structure
&3.5 switch multi choice structure
&3.1 if single selection structure
package struct; import java.util.Scanner; public class IfDemo1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please input:"); String s =scanner.nextLine(); //Equal: judge whether the string is equal if (s.equals("yee")){ System.out.println(s); } System.out.println("The best girl!"); scanner.close(); } }
&3.2 if double selection structure
package struct; import java.util.Scanner; public class IfDemo2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter the score:"); int i =scanner.nextInt(); if (i>90){ System.out.println("excellent"); }else{ System.out.println("come on."); } System.out.println("Inist Fighting!"); scanner.close(); } }
&3.3 if multiple choice structure
package struct; import java.util.Scanner; public class IfDemo3 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter the length of study:"); int i =scanner.nextInt(); if (i>=9){ System.out.println("Beipost is waiting for you"); }else if (i>=6 && i<9){ System.out.println("We should continue to work hard"); }else if (i>=4 && i<6){ System.out.println("You need more time to work hard"); }else { System.out.println("What you want can only be achieved through hard work!"); } System.out.println("Inist Fighting!"); scanner.close(); } }
&3.4 nested if structure
package struct; import java.util.Scanner; public class IfDemo4 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please input:"); String s =scanner.nextLine(); //Equal: judge whether the string is equal if (s.equals("yee")){ System.out.println(s); } System.out.println("The best girl!"); scanner.close(); } }
&3.5 switch multi choice structure
- The switch case statement is another way to implement the multi-choice structure.
- The switch case statement determines whether a variable is equal to a value in the - series values, and each value is called a branch.
package 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"); } } }
Support string type
package struct; public class SwitchDemo02 { public static void main(String[] args) { String name = "baby"; //New features of JDK7. The expression result can be a string! //The nature of characters is still numbers //Decompile java---class (Ningjie code file) -- decompile (IDEA) switch (name){ case "baby": System. out. println("yee"); break; case "You can do that!": System. out. println("Rush, rush"); break; default: System. out . println("A fool! "); } } }
Compiling and decompilingSource of learning materials: Crazy God says JAVA