java process control

Keywords: Java Algorithm

java process control

Scanner

Get the input string through the next() and nextLine() methods of the Scanner class. Before reading, you generally need to use hasNext() and hasNextLine() to judge whether there is still input data

Basic syntax:

Scanner s=new Scanner(System.in);
  • next():
  1. Be sure to read valid characters before you end the input
  2. next() cannot enter a string with a space (ending with a space)
public class Dome1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);       //Create a scanner object to receive keyboard data
        System.out.println("Please enter a string:");        
        if (scanner.hasNext()){       //Judge whether the user has entered a string            
            String str= scanner.next();       //Using the next mode, the program will wait for the user to input
            System.out.println("The input content is:"+str);
        }        
        scanner.close();       //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
    }
}
  • nextLine():
  1. End with Enter
  2. Can get blank
public class Dome2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter a string");
        if (scanner.hasNextLine()){
            String str= scanner.nextLine()  ;
            System.out.println("The output string is:"+str);
        }
        scanner.close();
    }
}

It can be simplified to:

public class Dome2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter a string");
            String str= scanner.nextLine()  ;
            System.out.println("The output string is:"+str);
        scanner.close();
    }
}

Common nextLine()

Little practice

  • Enter multiple numbers, find the sum and average. If you don't enter a number, press enter to confirm. End the input and output the execution result by entering and leaving non numbers
import java.util.Scanner;

public class Dome4 {
    public static void main(String[] args) {
        int sum=0;      //Definitions and variables
        int m=0;       //Calculate the number of numbers entered
        int a=0;       //Define the number entered
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter multiple numbers. Press enter for each number");
        while (scanner.hasNextInt()){
            a= scanner.nextInt();
            m=m+1;       //m++
            sum=sum+a;
        }
        System.out.println(m+"The sum of the two numbers is:"+sum);
        System.out.println(m+"The average of the numbers is:"+(sum/m));
        scanner.close();
    }
}

Sequential structure

Run from top to bottom

Select structure

if single selection structure

Syntax:

if(Boolean expression){
    //The statement that will be executed if the Boolean expression is true
}

if double selection structure

Syntax:

if (Boolean expression) {
    //The statement that will be executed if the Boolean expression is true
} else {
    //The statement that will be executed if the Boolean expression is false
}

if multiple selection structure

Syntax:

if (Boolean expression 1) {
    //Statement to execute if Boolean expression 1 is true
} else if (Boolean expression 2){
    //Statement to execute if Boolean expression 2 is true
}else if (Boolean expression 3){
    //Statement to execute if Boolean expression 3 is true
}else{
    //The statement that will be executed if the above Boolean expressions are false
}

Nested if structure

Syntax:

if (Boolean expression 1){
    //Statement to execute if Boolean expression 1 is true
    if (Boolean expression 2) {
        //Statement to execute if Boolean expression 2 is true
    }
}

switch multiple selection structure

The switch case statement determines whether a variable is equal to a value in a series of values. Each value is called a branch.

The variable types in the switch statement can be:

  • byte, short, int or char
  • Support String type
  • The case tag must be a string constant or literal

Syntax:

switch (expression){
    case value:
        //sentence
        break;       //Optional. It can be executed without adding, but penetration will occur
    case value:
        //sentence
        break;       //Optional. It can be executed without adding, but penetration will occur
    //You can have any number of case statements    
    default:       //Optional
        //sentence
}        

Puncture phenomenon:

char garde='2';
switch (garde){
    case '1':
        System.out.println("greater than");

    case '2':
        System.out.println("be equal to");

    case '3':
        System.out.println("less than");
        
    default:
        System.out.println("unknown");
}

char garde='2';
switch (garde){
    case '1':
        System.out.println("greater than");
        break;
    case '2':
        System.out.println("be equal to");
        break;
    case '3':
        System.out.println("less than");
        break;
    default:
        System.out.println("unknown");
}

When the corresponding value is matched, all statements before break will be output

Difference between if statement and switch statement

The if statement determines that the matching is an interval

The switch statement determines that a value matches

Cyclic structure

while Loop

Syntax:

while (Boolean expression){
    //Cyclic content
}

do... while loop

At least once

Syntax:

do{
    //Cyclic content
}while (Boolean expression);

The difference between while and do... While

while judge before execute, do... while execute before Judge

for loop

It is a general structure supporting iteration and the most effective and flexible loop structure

Syntax:

for (initialization; Boolean expression; to update){
    //Cyclic content
}
for(; ; ){       //Dead cycle structure
    
}

Little practice

  • Output the sum of odd and even numbers within 100 respectively
public class While {
    public static void main(String[] args) {
        int sum=0;
        int sum1=0;
        for (int i=0;i<=100;i=i+2){
            sum+=i;
        }
        for (int a=1;a<=100;a=a+2){
            sum1+=a;
        }
        System.out.println("100 The sum of even numbers within is"+sum);
        System.out.println("100 The sum of odd numbers within is"+sum1);
    }
}

  • Output the number that can be divided by 5 within 1 ~ 1000, and output 3 per line
public class While {
    public static void main(String[] args) {
        int sum=0;
        int sum1=0;
        for (int i=1;i<=1000;i++){
            if (i%5==0){
                System.out.print(i+"\t");
            }
            if (i%5*3==0){
                System.out.println();
            }
        }
    }
}

println will wrap after output

No line break after print output

  • Output 99 multiplication table
public class While {
    public static void main(String[] args) {
        for (int i = 1; i <= 9; i++) {
            for (int i1 = 1; i1 <= i; i1++) {
                System.out.print(i1 + "*" + i + "=" + i1 * i + "\t");
            }
            System.out.println();
        }
    }
}

Steps:

  1. Print first column
  2. Wrap the fixed 1 in a loop
  3. Remove duplicates
  4. Adjust style

Enhanced for loop

Syntax:

for (Declaration statement:expression){
    //Cyclic content
}

break continue

The main part of any loop can be terminated with break to forcibly exit the whole loop, but the program will not be terminated

continue is used to terminate a loop

practice

  • Drawing triangles exercise
public class Break {
    public static void main(String[] args) {
        for (int i = 1; i <=5; i++) {
            for (int j=5; j >=i; j--) {
                System.out.print(" ");
            }
            for (int j=2; j <=i; j++) {
                System.out.print("*");
            }
            for (int j=2; j <=i; j++) {
                System.out.print("*");
            }
           System.out.println("*");
        }
        }
}

Posted by at0mic on Sat, 16 Oct 2021 00:36:52 -0700