Java process control
1. User interaction Scanner
A toolkit for java util. Scanner can get user input
Basic syntax:
Scanner s = new Scanner(System.in);
Get the input string through the next() and nextLine() methods of the Scanner class. Before reading, we generally need hasNext() and hasNextLine() to judge whether there is any input data
import java.util.Scanner; public class hello { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("use next Reception mode:"); if (scanner.hasNext()){ String str = scanner.next();//Wait for input to complete System.out.println("The input content is"+str); } scanner.close(); } }
Finally, scanner close(); To close, all classes of I/O flow will always occupy resources if they are not closed at last
In next() and nextLine(), nextLine() will judge whether there is any input
import java.util.Scanner; public class hello { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("use nextLine Reception mode:"); if (scanner.hasNextLine()){ String str = scanner.nextLine(); System.out.println("The input content is"+str); } scanner.close(); } }
next
-
Be sure to read valid characters before you can end the input
-
The next method will automatically remove the blanks encountered before entering valid characters
//Input: hello world
//The blank before hello will be automatically removed
-
Only after entering a valid character will the blank space entered after it be used as a separator or terminator
//Input: hello world //Only hello will be output
-
The next method cannot get a string with spaces
nextLine
- Take Enter as the terminator, that is, the nextLIne method returns all characters before entering carriage return
- You can get blank, such as hello world
import java.util.Scanner; public class hello { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int i = 0; float f = 0.0f; if (scanner.hasNextInt()){ i = scanner.nextInt(); System.out.println(i); } scanner.close(); } } //scanner.hasNextInt() facilitates us to enter different data
Advanced use:
import java.util.Scanner; public class hello { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double sum = 0; int n = 0; while (scanner.hasNextDouble()){ double x = scanner.nextDouble(); n++; sum = sum + x; } System.out.println(sum); System.out.println(n); scanner.close(); } } /*4 4 4 4 4 g 20.0 5 The process has ended with exit code 0*/
2. Structure
2.1. Sequential structure
The basic structure of java is executed sentence by sentence
2.2. Select structure
2.2.1.if selection
2.2. 1.1. Single choice structure
if (Boolean expression){
/ / statement to be executed when Boolean expression is true
}
Someone might think of a? b: c. If you leave c blank and don't write it as an empty statement, isn't it a single choice result? But if you do, you will report an error, so try to avoid this problem.
import java.util.Scanner; public class hello { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); //equals: judge whether the strings are equal. Note that = = is less used to judge whether the strings are equal if (s.equals("hello")){ System.out.println(s); } System.out.println("End"); scanner.close(); } }
2.2. 1.2. Double choice structure
if(){
}
else{
}
2.2. 1.3. Multiple choice structure
if (Boolean expression 1){
Code 1
}else if (Boolean expression 2){
Code 2
}Else if (Boolean expression 3){
Code 3
}else{
Code 4
}
Although there are many options, only one situation can be carried out at a time.
2.2. 1.4. Nested structure
if (Boolean expression 1){
if (Boolean expression 2){
Code
}
}
2.2.2.switch multi selection structure
switch(expersion){
case value: Code 1;break;//break optional
case value: Code 2;break;//break optional
case value: Code 3;break;//break optional
default: Code 4// This statement is optional
}
The cass tag must be a string constant or literal
The variable types in the switch statement can be byte,short,int,char,String
After compiling, the java file becomes a class file (bytecode file), which can be viewed through the open folder where the idea decompile file is located
2.3. Cyclic structure
2.3.1.while loop
while (Boolean expression){
/ / loop content
}
As long as the Boolean expression is true, it will continue.
2.3.2.do... while loop
do{
/ / loop content
}while (Boolean expression);
This loop is executed at least once
2.3.3.for loop
for (initialization; Boolean expression; update){
/ / loop content
}
Initialization, Boolean expression and update can all be empty statements, but they may be an endless loop
for(int i=0;i<100;i++){ int sum = sum + 1; int num += 2;//iteration }
2.3. 4. Enhanced for loop
The enhanced for loop is mainly used for arrays and collections
For (declaration statement: expression){
/ / code sentence
}
Declaration statement: type matches array element type. Its value is equal to the value of the array element at this time
Expression: the name of the array to access, or the method whose return value is array
int[] numbers = {10,20,30,40,50};//Defines an array for (int x: numbers) {system. Out. Println (x);} for(int i = 0;i<5;i++){ System.out.println(numbers[i]);}// The two effects are the same, but the former is simpler
2.3.5.break and continue
-
break can use its control flow in the main part of any loop statement to forcibly exit the loop without executing the remaining statements in the loop (also used in switch)
-
continue is also used to terminate a cycle and proceed to the next cycle
-
goto keyword:
public class hello { public static void main(String[] args) { outer:for (int i = 0;i<5;i++){ for (int j = 0;j<5;j++){ System.out.println(j); if (i==j){ continue outer; } } } }}//The above is the usage of goto keyword, which is similar to goto statement in c language
The writing method is troublesome and is not recommended