Java program yuan

Posted by ClarkF1 on Tue, 04 Jan 2022 18:28:27 +0100

Tell the blogger @ of station b to meet the crazy God all the way
Java basic video

1, User interaction Scanner
1. Realize the interaction between programs and people. java provides us with a tool class, so we can get the user's input, and we can get the user's input through the Scanner class.
2. Basic grammar:

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 to use hasNext() and hasNextLine() to judge whether there is still input data.
3.next()
(1) You must read valid characters before you can end the input;
(2) The next() method will automatically remove the white space encountered before entering valid characters;
(3) Only after a valid character is entered, the blank space entered after it will be used as a separator or terminator;
(4) next() cannot get a string with spaces.

4.nextLine()
(1) Take Enter as the ending character, that is, the nextLine() method returns all characters before entering carriage return;
(2) You can get blank.

5. Advanced usage of scanner

2, Sequential structure
1. The basic structure of Java is sequential structure, which is the simplest algorithm structure.
2. Statements are carried out from top to bottom. It is composed of several processing steps executed in turn. It is a basic algorithm structure that any algorithm is inseparable from.

3, Select structure
1.if single selection structure
Judge whether a thing is feasible before implementing it
Syntax:

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

  1. if double selection structure
    true: execute a statement
    false ------ execute another program
    If else structure
    Syntax:
if(Boolean expression){
	//The Boolean expression has a value of true
}else{
	//The Boolean expression has a value of false
}


2. if multiple selection structure
There are many situations such as ABCD... And interval judgment
Syntax:

if(Boolean expression 1){
	//Code executed when the value of Boolean expression 1 is true
}else if(Boolean expression 2){
	//Code executed when the value of Boolean expression 2 is true
}else if(Boolean expression 3){
	//Code executed when the value of Boolean expression 3 is true
}......
{
	//The code executed when the value of Boolean expression n-1 is true
}else(Boolean expression n){
	//Code executed when the value of Boolean expression n is true
}


3. Nested if structure
You can use an if or else if statement in another if or else if statement, which can be nested so that

if(Boolean expression 1){
	//Code executed when the value of Boolean expression 1 is true
	if(Boolean expression 2){
		//Code executed when the value of Boolean expression 2 is true
	}
}

For example, you want to find a number between 1-100

4. switch multi selection structure
(1) The swich case statement determines whether a variable is equal to a value in a series of values. Each value is called a branch.
(2) The variable types in the switch statement can be:
byte, short, int or char
Starting with Java SE7, String types can be supported
At the same time, the case tag must be a string constant or literal
(3) Syntax:

switch(expression){
	case value:
	//sentence
		break;//Optional
	case value:
	//sentence
		break;//Optional
	//You can have any number of case statements
	default://Optional
		//sentence		
}



How to find out where to decompile:







5.while loop
(1) The most basic loop, the loop will always execute
(2) Grammatical structure

while(Boolean expression){
	//Cyclic content
}

(3) As long as the value of the Boolean expression is true, the loop will continue to execute
(4) In most cases, the loop is stopped, and a way to invalidate the expression is needed to end the loop
(5) A few will keep the loop going, such as the response of the server
(6) Avoid dead circulation
(7) Thinking: 1 + 2 + 3 + 4 +... + 100 =?


6. do... while loop
(1) Even if a condition is not met, it must be executed at least once
(2) Similar to while, the difference is that the do... While loop executes at least once
(3) Syntax structure:

do{
	//Code statement
}while(Boolean expression);

(4) The difference between while and do... While:
while judge before execute, do... while execute before judge, do... while always loop will be executed at least once

7.for loop
(1) for loop statement is a general structure that supports iteration. It is the most priority and flexible loop structure.
(2) The number of times the for loop is executed is determined before execution
Syntax structure:

for(initialization; Boolean expression; to update){
	//Code statement
}

Exercise: (1) calculate the sum of odd and even numbers between 0-100.

(2) Use the while or for loop to output the number that can be divided by 5 between 1-1000, and output 3 per line.

(3) Print the 99 multiplication table.

7. Enhanced for loop
(1) In the for loop that traverses arrays and collections
(2) Syntax format:

for(Declaration statements: expressions){
	//Code sentence
}

(3) Declaration statement: declare a new local variable. The type of the variable must match the type of the array element. Its scope is limited to the circular statement block, and its value is equal to the value of the numeric element at this time.
(4) Expression: is the name of the array to access

8.break and 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 without executing the remaining statements in the loop. (break statements are also used in switch statements).
(2) The continue statement is used in the loop statement body to terminate a loop process, that is, jump out of the statement that has not been executed in the loop body, and then determine whether to execute the loop next time.

9.goto keyword
You can see the shadow of goto on the two keywords break and continue

4, Method
1. What is the method?
A Java method is a collection of statements that together perform a function.

Call the println() method in the standard output object out in the system class
(1) It is an orderly combination of steps to solve a class of problems;
(2) Methods are contained in classes or objects;
(3) Methods are created in the program and referenced elsewhere.
2. Principle of design method: a method only completes one function, that is, maintaining the atomicity of the method.

3. Method definition syntax
A method contains a method header and a method body. The following are all parts of a method:
(1) Modifiers, such as public, static, final, etc. It is optional and can be written or not
(2) Return value type: the data type of the return value. If there is no return value, void is used
(3) Method name: actual name
(4) Parameter type: formal parameter and argument, like a placeholder
(5) Method body

Modifier return value type method name (parameter type parameter name){
	....
	Method body
	....
	return Return value
}


Value passing and reference passing??

4. Overloading of methods
(1) Overloading is a function with the same function name but different formal parameters in a class.
(2) Rules for overloaded methods:
Method name must be the same;
The parameter list must be different (number or type or parameter arrangement order is different);
The return types of methods can be the same or different;
Just different return types are not enough to overload methods.

5. Command line parameter transmission
Messages are passed to a program when it runs. This is achieved by passing command-line arguments to the main() function.

6. Variable parameters
(1) Variable parameters of the same type can be passed.
(2) In the method declaration, add an ellipsis (...) after specifying the parameter type.
(3) Only one variable parameter can be specified in a method. It must be the last parameter of the method. Any ordinary parameter must be declared before it.

7. Recursion (it will occupy a lot of space and memory, but it is convenient to write programs. You can use it if you don't need it. It can be used if the cardinality is small)
(1) Method A calls method A and calls itself;
(2) Using recursion, we can solve some complex problems with simple programs. It usually transforms a large and complex problem layer by layer into a smaller problem similar to the original problem. The recursive strategy can describe the multiple repeated calculations required in the problem-solving process with only a small amount of program, which greatly reduces the amount of code of the program. The power of recursion is to define an infinite set of objects with limited statements.
(3) Recursion consists of two parts:
Part I
Recursive header: indicates when to not call its own method. If there is no head, it will fall into a dead cycle.
Part II
Recursive body: when to call its own method.

8. Write a calculator, which is required to realize the function of addition, subtraction, multiplication and division, and can receive new data circularly, which can be realized through user interaction
Recommended ideas:
(1) Write four methods: addition, subtraction, multiplication and division;
(2) User interaction using loop + switch;
(3) Pass two numbers to be operated;
(4) Output results

import java.util.Scanner;

public class Demo07 {
    // Write a calculator, which is required to realize the function of addition, subtraction, multiplication and division, and can receive new data circularly, which can be realized through user interaction
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter the first number:");
        double n1 = scanner.nextDouble();
        System.out.println("Please enter the second number:");
        double n2 = scanner.nextDouble();
        System.out.println("Please enter an operator(+,-,*,/):");
        String opertor = scanner.next();
        switch (opertor){
            case "+":
                System.out.println("The calculation result is:"+add(n1,n2));
                break;
            case "-":
                System.out.println("The calculation result is:"+cut(n1,n2));
                break;
            case "*":
                System.out.println("The calculation result is:"+multiply(n1,n2));
                break;
            case "/":
                System.out.println("The calculation result is:"+divide(n1,n2));
                break;
            default:
                System.out.println("Operator input error!!");
        }
        scanner.close();
    }
    public static double add(double num1,double num2){
        double sum = num1+num2;
        return sum;
    }
    public static double cut(double num1,double num2){
        double reduce = num1-num2;
        return reduce;
    }
    public static double multiply(double num1,double num2){
        double mult = num1*num2;
        return mult;
    }
    public static double divide(double num1,double num2) {
        double divi = 0;
        if (num2 != 0) {
            divi = num1 / num2;
        }else {
            System.out.println("The second number cannot be 0!!");
        }
        return divi;
    }
}

Topics: Java Back-end