Detailed explanation of Java process control

java process control

I User interaction Scanner

scanner object

  1. Scanner class to get user input

  2. Basic grammar

    //Create a scan object to receive keyboard data
    Scanner scanner=new Scanner(System.in);
    System.out.println("use next Reception mode:");
    //Judge whether the user has entered a string
    if (scanner.hasNext()){
      //Receive in next mode
      String str=scanner.next();//The program waits for user input to complete
      System.out.println("The output contents are:"+str);
    }
    scanner.close();//Turn off the scanner
    //All classes belonging to the IO stream will always occupy resources if they are not closed. Make a good habit of closing them when they are used up
    
    - next():The input can be ended only after reading the valid characters. For the blank encountered before entering the valid characters, next()Method will automatically remove it,*next()Cannot get a string with spaces* 
    
    - nextLine(): with enter Is the terminator, that is to say nextLine()Method returns all characters before entering carriage return, which can be blank
    
      ```java
      Scanner scanner=new Scanner(System.in);
      System.out.println("Please enter data:");
      String str=scanner.nextline();
      System.out.println("The output content is:"+str);
      scanner.close();
    

scanner advanced

Scanner scanner=new Scanner(Syetem.in);
int i=0;
float f=0.0f;
System.out.println("Please enter an integer:");
//Judge whether the input is an integer
if(scanner.hasNextInt()){
  i=scanner.hasNextInt();
  System.out.println("Integer data:"+i);
}else{
  System.out.println("The input is not integer data!");
}
//Judge whether it is a decimal, hasNextFloat, the same usage

case

//We can enter multiple numbers and find the sum and average of them. Press enter to confirm each number. Enter a non number to end the input and output the execution result
Scanner scanner=new Scanner(System.in);
double sum=0;
int m=0;//Calculate how many numbers are entered
//Judge whether there is any input through the loop, and sum and count each time in it
while(scanner.hasNextDouble()){
  double x=scanner.nextDouble();
  m++;
 sum+=x; 
}
System.out.println(m+"The sum of the numbers is"+sum);
System.out.println(m+"The average number is"+(sum/m));
scanner.close();

II Sequential structure

  1. The basic structure of java is sequential structure

  2. Between statements and between boxes, it is in the order from top to bottom. It is the simplest algorithm structure

III Select structure

if selection structure

  1. if single choice structure
  • Syntax format: if (Boolean expression) {conform to the execution of the expression}

  • 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();
    
  1. if double selection structure
  • case

  • //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();
    
  1. if multiple selection structure
  • Grammatical structure

if (Boolean expression 1){

}else if (Boolean expression 2){

}else if (Boolean expression 3){

}else{

}

switch multiple selection structure

  1. Grammatical structure
switch(expression){
  case value:
    //sentence
    break;
  case value:
    //sentence
    break;
  default://Optional
    //sentence
}

//The variable type in the switch statement can be byte, short, int, char or string (supported after Java SE7)

switch is used to match a value / case

  1. case
String name="Mad God";
//JDK7 new features, the expression result can be a string!
switch(name){
    case"Qin Jiang": 
      System.out.println("Qin Jiang");
      break;
    case"Mad God":
    	System.out.println("Mad God");
    	break;
  default:
    System.out.println("error");
    
    
}

IV Cyclic structure

while Loop

  1. while (Boolean expression) {/ / loop content}
  2. In most cases, we will stop the loop. We need a method to invalidate the expression to end the loop
  3. case
//Output 1 ~ 100
int i=0;
while(i<100){//Cannot add equal to. It starts from 0
  i++;
  System.out.println(i);
}
//Calculate 1 + 2 ++ 100=?
int i=0;
int sum=0;
while(i<=100){
  sum+=i;
  i++;
}
System.out.println(sum);


do... while loop

  1. Grammatical structure
do{//Code statement
}while(Boolean expression)
  1. while judges before execution, and do... while executes before judgment, that is, the do... while loop will be executed at least once
  2. case
int i=0;
int sum=0;
do{
  sum+=i;
  i++;
}while(i<=100);

for loop

  1. Grammatical structure
for(initialization; Boolean expression; to update){
  //Code statement
}
  1. case
//1. Calculate the sum of all odd and even numbers between 0-100
int oddSum=0;//Odd sum
int evenSum=0;//Even sum
for(int i=0;i<=100;i++){
  if(i%2!=0){
    oddSum+=i;
  }else{
    evenSum+=i;
  }
}
System.out.println("The sum of odd numbers is:"+oddSum);
System.out.println("The sum of even numbers is:"+evenSum);


//2. Output the number that can be divided by 5 between 1-1000, and output 3 for each line
for(int i=0;i<=1000;i++){
  if(i%5==0){
    System.out.print(i+"\t");
  }//println: Line wrap after output
  //print does not wrap after output
  if(i%(5*3)==0){
    System.out.print("\n");
  }
}

//3. Print 99 multiplication table
for(i=1;i<=9;i++){
  for(j=1;j<=i;j++){
    System.out.print(i+"*"+j+"="+(i*j)+"\t");
    
  }
  System.out.println("\n");
}


  1. be careful
  • The initialization step is executed first, which can be an empty statement
  • Then detect the value of the Boolean expression
  • Then start executing the loop body
  • Finally, update the loop control variable
  1. Enhanced for loop (for arrays)
int[]numbers={10,20,30,40,50};//Define an array
for(int x:numbers){
  System.out.println(x);
}//Traversing the elements of an array
************************************
for(int i=0;i<5;i++){
System.out.println(number[5]);
}

V break&continue

  1. Break in the body of any loop statement, you can use break to control the flow of the loop. Break is used to forcibly exit the loop. Do not execute the remaining statements in the loop
  2. The continue statement in the loop statement body is used to terminate a loop process, that is, skip the statements that have not been executed in the loop body, and then execute the judgment of whether to execute the loop next time
  3. About goto keyword
  • java does not have goto, but the shadow of goto can still be seen in break and continue - labeled break and continue
//Case: printing triangles
for(int i=1;i<=5;i++){
  for(int j=5;j>=i;j--){
    System.out.print(" ");
  }
  for(int j=1;j<=i;j++){
    System.out.print("*");
  }
  for(int j=1;j<i;j++){
    System.out.print("*");
  }
}

Keywords: Java Back-end JavaSE

Added by imamferianto on Thu, 20 Jan 2022 19:45:32 +0200