Java Lesson Four

First, the branch statement
1.if basic sentence format

if(布尔表达式){
    
    
      //条件满足时执行代码
}

*if can only be a boolean expression, that is, it is true, otherwise it is not executed

if(布尔表达式){
    
    
       //条件满足时执行代码
}else{
    
    
       //条件满足时执行代码
}

③Multi-branch situation

if(布尔表达式){
    
    
       //条件满足时执行代码
}else if(布尔表达式){
    
    
       //条件满足时执行代码
}……
else{
    
    
       //条件满足时执行代码
}

Leap year: century leap year (divided by 400)
ordinary leap year (divided by 4 or 100)
④Cautions
I. Dangling else problem The
if/else statement does not need to increase the brackets, but the else matches the nearest if (only one statement can be written), Therefore, it is not recommended to
use indentation to represent code blocks in pathon in this way
. Ⅱ. Code style issues. It is
recommended to write curly brackets. How to write is determined by the company or currently written as the first half of the curly brackets. The style that does not start a new line
after the if. Adding a semicolon is equivalent to executing a semicolon, that is, an empty statement, so the statement in the braces has nothing to do with the if, regardless of whether the Boolean expression is true or false, the statement in the braces will be executed
2.switch statement

switch(整数,枚举,字符,常量){
    
    
     case内容1:{
    
    
         内容满足时执行语句;break;}
…………
      default: {
    
    
        执行语句;break;}
}

The use of switch statement has some limitations
①switch () can only put a limited number of types
②break, once the program execution effect is omitted, errors may occur ③difficult
to express complex logic ④Although
nesting is supported, it is very ugly
* Only use in switch BREAK, can not continue
II loop structure
1.while cycle

while(循环条件){
    
    
        执行语句;
}

Process finished with exit code 0 / -1
Process exit code It is
generally agreed that the exit code of 0 is the normal end of the program, otherwise it is abnormal termination.
* Cache and buffer are essentially two things that have no relationship.
Cache
buffer (buffer) computer The efficiency of accessing external memory (hard disk) is much lower than that of accessing memory (three to four orders of magnitude). With the help of buffers, frequent external memory accesses can be used to reduce the number of times. The
buffers are essentially the use of space for time to improve program efficiency
. Use shift+F6 to trigger the rename function (quickly rename variables/methods/classes)
③IDEA debugging is to make the program run slower, so that programmers can observe the intermediate results of the code.
Breakpoint (Debug)
2.
break and continue break Is to let the loop end early, that is, to jump out of the loop when it encounters a break, which means to end the entire loop,
continue means to end the current loop
3. for loop

for(表达式1;表达式2;表达式3{
    
    
                  循环体;
}

Expression 1 is the initialization statement of the loop variable, which is only executed once when entering the for loop; expression 2 is the loop condition, executes the loop body for true, and ends the loop for false; executes the loop body; expression 3 is used to update the loop variable
for The loop brings the three elements together to prevent omissions
4.do while loop

do{
    
    
      循环语句;
}while(循环条件);

There must be a semicolon after do while, otherwise there will be an error
fori => tab code template
3. Input and output
1. Output
println wrap
print without wrapping
printf format
system.out
standard output, essentially a file
standard input keyboard system.in
standard system.out output display
standard error display System.err
2.scanner is a standard Java class libraries provided in
class is a "high with structure", is a custom type, the argument is an object created out of the defined type
① first Create a scanner object with system.in as a parameter

Scanner scanner = new Scanner();
             关键字实例化对象

②Use the import statement to import the Scanner class.
Write import java.util.Scanner at the top;
you must write the import statement.
③Use the scanner.nextXXX() method to read the data, XXX represents the data type

int num = scanner.nextInt();
double num= scanner.nextDouble();

Guess you like

Origin blog.csdn.net/WSXHN/article/details/112966482