Java process control
User interaction Scanner
The java.util.Scanner package provides the Scanner to obtain user input
Scanner s=new Scanner(System.in)
scanner.close()
package Scanner; import java.util.Scanner; public class Demo01 { public static void main(String[] args) { //Create a scanner object to accept keyboard data Scanner scanner=new Scanner(System.in); // System.out.println("receive in next mode:"); System.out.println("use nextLine Reception mode:"); //Judge whether the user has entered a string if(scanner.hasNextLine()){ //Receive in next mode // String str=scanner.next(); String str=scanner.nextLine(); System.out.println("The output contents are:"+str); } //All classes belonging to IO streams will always occupy resources and form good habits if they are not closed 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 the valid characters entered
Only after entering a valid character will the space entered after it be used as a separator or terminator
next() cannot get a string with spaces
nextLine():
With Enter as the terminator, the nextLine() method returns all characters before the input carriage return
You can get spaces
example:
package Scanner; import java.util.Scanner; public class Demo02 { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int i=0; float f=0.0f; System.out.println("Please enter an integer:"); //Judge whether the user has entered an integer if(scanner.hasNextInt()){ i=scanner.nextInt(); System.out.println("The output contents are:"+i); } else{ System.out.println("The input is not integer data!"); } if(scanner.hasNextFloat()){ f= scanner.nextFloat(); System.out.println("Decimal data:"+f); } else{ System.out.println("The input is not decimal data"); } //All classes belonging to IO streams will always occupy resources and form good habits if they are not closed scanner.close(); } }
Sequential structure
Step by step, from top to bottom
Select structure
if radio structure
if(Boolean expression){ //If the Boolean expression has a value of true }
if double selection structure
if(Boolean expression){ //If the Boolean expression has a value of true }else{ //If the value of the Boolean expression is false }
if multiple selection structure
if(Boolean expression 1){ //If the value of Boolean expression 1 is true }else if(Boolean expression 2){ //If the value of Boolean expression 2 is true }else if(Boolean expression 3){ //If the value of Boolean expression 3 is true }else{ //If none of the above Boolean expressions is true }
switch selection structure
-
The switch case statement determines whether a variable is equal to a value in a series of values
-
The variable type can be byte, short, int or char, and the String type (starting from Java SE 7) is supported
-
The case tag must be a string constant or literal
switch(expression){ case value: //sentence breakļ¼//Optional case value: //sentence break;//No break will output all of the following //You can use any number of case statements default://Optional //sentence }
example
public class Switch01 { public static void main(String[] args) { char grade='C'; //case penetration, matching a specific value 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("unqualified"); break; default: System.out.println("Unknown level"); } } }
Cyclic structure
while Loop
Judgment before execution while(Boolean expression){ //Cyclic content }
do while loop
Execute before Judge
Execute at least once even if the conditions are not met
do{ //Code statement }while(Boolean expression)
for loop
for(initialization;Boolean expression;to update){ //Code statement } //Dead cycle for(;;){ }
Quick input 100.for
for (int i = 0; i < 100; i++) { }
example
public class ForDemo01 { public static void main(String[] args) { //Calculate 1 to 100 int a=1;//Initialization condition int sum=0; while(a<=100){//Conditional judgment sum=sum+a; a++; } System.out.println(sum); System.out.println("while End of cycle!"); sum=0; //Initialize. / / judge Boolean values. / / update iteratively for(int i=1;i<=100;i++) { sum=sum+i; } System.out.println(sum); System.out.println("for End of cycle!"); } }
Print 99 multiplication table
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
public class ForDemo02 { //Output 99 multiplication table public static void main(String[] args) { for(int j=1;j<=9;j++){ for(int i=1; i<=j;i++){ System.out.print(i+"*"+j+"="+(j*i)); System.out.print('\t'); } System.out.println(); } } }
Enhanced for loop
It is mainly used to traverse arrays and collection objects
for(Declaration statements: expressions) { //Code statement }
example
public class ForDemo03 { public static void main(String[] args) { //Define an array int[] numbers={10,20,30,40,50}; //The following two outputs are equal for (int i=0;i<5;i++){ System.out.println(numbers[i]); } System.out.println("=============="); //Traverse array elements for(int x:numbers){ System.out.println(x); } } }
break and continue
break: it can be used in the main part of any loop statement. break is used to forcibly exit the loop, do not execute the remaining statements in the loop, and do not terminate the program (terminate the loop)
continue: used in the body of a loop statement. It is used to terminate a loop process, skip the unexecuted statements in the loop body, and then determine whether to execute the loop next time (without terminating the loop)
No goto keyword
The difference between print and println
print will not wrap lines after output, and it will be output all the time
println will wrap after output
System.out.print('\n'); System.out.println();//The two statements are equivalent