Class notes on java process control
1. User interaction Scanner
Scanner object
-
The Scanner class in the java.util.Scanner package gets the user's input
-
Basic syntax:
Scanner scanner = new Scanner(System.in);
-
Get the input string through the next() and nextLine() methods of the Scanner class. Before reading, we usually need to use hasNext() and hasNextLine() to determine whether there is any input data
next(): 1.You must read valid characters before you can end the input 2.For blanks encountered before entering valid characters, next()Method will be removed automatically 3.The space entered after a valid character is used as a separator or terminator only after it is entered 4.next()Cannot get string with space nextLine(): 1.with Enter Is the closing character, i.e nextLine()Method returns all the characters before enter 2.Can get blank
Example code
import java.util.Scanner; public class Test{ public static void main(String[] args){ //Create a scanner object to receive keyboard data Scanner scanner = new Scanner(System.in); System.out.println("Use next Received by:"); //Judge whether the user has input string if(scanner.hasNext()){ String str = scanner.next(); //Input: 123 123 123 System.out.println(str);} //Output: 123 //If you do not close the io stream class, it will occupy resources all the time. You should form a good habit of closing it when you use it scanner.close(); } }
import java.util.Scanner; public class Test{ public static void main(String[] args){ //Create a scanner object to receive keyboard data Scanner scanner = new Scanner(System.in); System.out.println("Use nextLine Received by:"); //Judge whether the user has input string if(scanner.hasNextLine()){ String str = scanner.nextLine(); //Input: 1234 1234 12 System.out.println(str);} //Output: 1234 1234 12 //If you do not close the io stream class, it will occupy resources all the time. You should form a good habit of closing it when you use it scanner.close(); } }
2.Scanner advanced
Example code
import java.util.Scanner; public class Test{ public static void main(String[] args){ /*Demand: input multiple numbers and find their sum and average value, For each data input, the input is ended and the result is output by inputting non digital data*/ Scanner scanner = new Scanner(System.in); int i = 0; double sum = 0; System.out.println("Please enter data:"); while(scanner.hasNextDouble()){ double d = scanner.nextDouble(); i++; sum = sum + d; System.out.println("Current input"+i+"The sum of data is"+sum); } System.out.println(i+"The sum of the numbers is"+sum); System.out.println(i+"The average number is"+(sum/i)); scanner.close(); } }
3. Three algorithms of Java
Sequential structure
The basic algorithm structure of java
Selection structure
- if single selection structure
If (Boolean expression){ //Statement executed when Boolean expression is true }
- if double selection structure
if(Boolean expression){ //Statement executed when Boolean expression is true }else{ //Statement executed when Boolean expression is false }
- if multi selection structure
if(Boolean expression 1){ //Statement executed when Boolean expression 1 is true }else if(Boolean expression 2){ //Statement executed when Boolean expression 2 is true }else if(Boolean expression 3){ //Statement executed when Boolean expression 3 is true }else{ //Execute code if none of the above Boolean expressions are true }
Example code
import java.util.Scanner; public class Test{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter your score:"); int score = scanner.nextInt(); if(score == 100){ System.out.println("Congratulations on your full score."); }else if(score < 100 && score >= 90){ System.out.println("excellent"); }else if(score < 90 && score >= 80){ System.out.println("good"); }else if(score < 80 && score >= 60){ System.out.println("pass"); }else if(score < 60 && score >= 0){ System.out.println("Fail,"); }else{ System.out.println("Illegal input result"); } scanner.close(); } }
- Nested if structure
If (Boolean expression 1){ //Execute code if the value of Boolean expression 1 is true If (Boolean expression 2){ //Execute code if the value of Boolean expression 2 is true } }
- switch multi choice structure
switch(expression){ case value1: //Sentence break;//Optional case value2: //Sentence break;//Optional //... can have any number of case statements default: //Optional //Sentence }
The variable types in the switch case statement can be:
- byte, short, int or char
- Starting from jdk7, switch supports String type
- case label must be a string constant or literal
public class Test{ 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 level"); } } }
Decompile with IDEA: copy and paste the Test.class file into the package of Test.java
Cyclic structure
- while Loop
While (Boolean expression){ //Cycle content }
Note: as long as the Boolean expression is true, the loop will continue to execute, forming a dead loop
Example code
/*Demand: calculate the sum of 1-100*/ public class Test{ public static void main(String[] args) { int i = 1; int sum = 0; while(i <= 100){ sum = sum + i; i++; } System.out.println("As for"+sum); //And 5050 } }
- do... while Loop
do{ //Code statement }while(Boolean expression);
The difference between while and do while:
1. When is execute after judgment, while is execute before judgment
2. do... while always ensures that the loop is executed at least once
Example code
/*Demand: calculate the sum of 1-100*/ public class Test{ public static void main(String[] args) { int i = 1; int sum = 0; do{ sum = sum + i; i++; }while(i <= 100); System.out.println("As for"+sum); //And 5050 } }
- for cycle
for loop statement is a general structure supporting iteration, which is the most effective and flexible loop structure
For (initialization; Boolean expression; update){ //Code statement }
Instance code 1
/*Requirements: calculate odd sum and even sum of 1-100 respectively*/ public class Test{ public static void main(String[] args) { int oddsum = 0; int evensum = 0; for (int i = 0; i <= 100; i++) { if(i%2==1){ oddsum += i; }else{ evensum += i; } } System.out.println("The sum of odd numbers is"+oddsum); //The sum of odd numbers is 2500 System.out.println("Even sum is"+evensum); //The sum of even numbers is 2550 } }
Instance code 2
/*Demand: multiplication table*/ public class Test{ public static void main(String[] args) { for (int i = 1; i < 10; i++) { for (int j = 1; j <= i; j++) { System.out.print(i+"*"+j+"="+i*j+"\t"); } System.out.println(); } } }
- Introducing an enhanced for loop for arrays in Java 5
For (declaration statement: expression){ //Code sentence } **Declaration statement * *: declare a new local variable whose type must match the type of array element. Its scope is limited to the cyclic statement block, and its value is equal to the value of array element at this time **Expression * *: expression is the name of the array to be accessed, or the method whose return value is array
Example code
public class Test{ public static void main(String[] args) { int[] numbers = {10,20,30,40,50};//An array is defined //Traversing elements of an array for (int number : numbers) { System.out.println(number); } } }
4.break,continue
break usage
- In the main part of any loop statement, break can be used to control the flow of the loop
- Used to forcibly exit the loop without executing the remaining statements in the loop
continue usage
- Statement is used in the body of a circular statement to terminate a circular process, i.e. skip the statement that has not been executed in the body of the circular, and then determine whether to execute the circular next time
5. Print triangle and Debug
Example code
public class Test{ /*Print triangles*/ public static void main(String[] args) { for (int i = 0; i <= 5; i++) { for(int j = 5; j >= i; j--){ System.out.print(" "); } for(int j = 0; j <= i; j++){ System.out.print("*"); } for(int j = 1; j <= i; j++){ System.out.print("*"); } System.out.println(); } } }
- Debug: debugging, you can execute the abbreviated code step by step, and explain later