Assignment operator=
public class Project2 { public static void main(String[] args) { //Assign 10 to variable i of type int int i=10; System.out.println("i:"+i); // +=Add the data on the left and right, and assign the result to the left i += 20; //It can be replaced by i=i+20 System.out.println("i:"+i); short s=10; //s+=20; The underlying of the extended assignment operator implies cast s=(short)(s+20); System.out.println("s:"+s); } }
/*F:\>javac Project2.java F:\>java Project2 i:10 F:\> */
F:\>javac Project2.java F:\>java Project2 s:30 F:\>
+=Add and assign to the left
-=Assign to the left after subtraction
*=Multiply and assign to the left
/=Assign to the left after division
%=The remainder is assigned to the left
Note: the extended assignment operator implies a cast
Self increasing and self decreasing operator
++ Add 1 to the value of the self increasing variable
-- Self subtraction The value of the variable minus 1
public class Project2 { public static void main(String[] args) { //Define variables int i=10; i++; System.out.println("i:"+i); } }
F:\>javac Project2.java F:\>java Project2 i:11 F:\>
public class Project2 { public static void main(String[] args) { //Define variables int i=10; int j=i++; System.out.println("i:"+i); System.out.println("j"+j); } }
F:\>javac Project2.java F:\>java Project2 i:11 j10
public class Project2 { public static void main(String[] args) { //Define variables int i=10; int j=++i; System.out.println("i:"+i); System.out.println("j"+j); } }
F:\>javac Project2.java F:\>java Project2 i:11 j11 F:\>
matters needing attention:
++And -- can be placed either after or before the variable
When used alone, the results of + + and -- are the same before and after
When participating in the operation, put it after the variable, first take the participating variable for operation, and then take the variable as + + or--
When participating in the operation, before the variable, take the variable as + + or --, and then take the variable to participate in the operation
Relational operator
==Are they equal
!= Are they unequal
>= <= < >
True, not fasle
The output results of relational operators are of boolean type
Logical operator: an operator used to connect relational expressions, or directly connect Boolean constants or variables
& Logical and (false out of false)
| Logical or (true if true)
^Logical XOR (false for the same but true for the different)
! Logical non (opposite result)
public class Project2 { public static void main(String[] args) { //Define variables int i=10; int j=20; int k=30; // & System.out.println((i>j)&(i>k)); //false&false } }
F:\>javac Project2.java F:\>java Project2 false
Short circuit logic operator
&&Short circuit and when the left is false, the right is not executed
|| Short circuit or When the left is true, the right is not executed
public class Project2 { public static void main(String[] args) { //Define variables int i=10; int j=20; int k=30; // && System.out.println((i>j)&&(i>k)); } }
F:\>javac Project2.java F:\>java Project2 false
Ternary operator
Format: relational expression? Expression 1: expression 2;
Example: a > b? A: B
Calculation rule: first calculate the value of the relationship expression
If the value is ture, the value of expression 1 is the operation result;
If the value is false, the value of expression 2 is the operation result;
public class Project2 { public static void main(String[] args) { //Define variables int a=10; int b=20; // & int max=a>b?a:b; //Output results System.out.println("max"+max); } }
F:\>javac Project2.java F:\>java Project2 max20
data input
Basic steps for using Scanner
1. Guide Package import java.util.Scanner; (the action of package guide must appear above the class definition)
2. Create object Scanner sc = new Scanner(System.in) (in this format, only SC is the variable name, which can be changed, and others are not allowed to be changed)
3. Data acceptance int i = sc.nextInt() (in this format, only I is the variable name, and others are not allowed to be changed)
import java.util.Scanner; public class Project2 { public static void main(String[] args) { //create object Scanner sc = new Scanner(System.in); //receive data int x = sc.nextInt(); //output data System.out.println("x:"+x); } }
F:\>javac Project2.java F:\>java Project2 10 x:10 F:\>
Process control
Process control statement classification
Sequential structure
Branch structure (fi, switch)
if statement format 1
If (relational expression){
Statement body;
}
if statement format 2
If (relational expression){
Statement body 1;
}else{
Statement body 2;
}
Cyclic structure (for, while, do...while)
import java.util.Scanner; //Guide Package public class Project2 { public static void main(String[] args) { //create object //Scanner sc = new Scanner(System.in); Scanner i = new Scanner(System.in); //receive data //int x = sc.nextInt(); int x = i.nextInt(); //Judge data parity if(x%2==0) { System.out.println("The number is even"); }else{ System.out.println("The number is odd"); } //output data //System.out.println("x:"+x); } }
F:\>javac Project2.java F:\>java Project2 11 The number is odd
if statement format 3
Format:
if (relational expression 1){
Statement body 1;
}else if (relational expression 2){
Statement body 2;
}
...
else{
Statement body n;
}
switch statement format
switch (expression){
case value 1:
Statement body 1;
break;
case value 2:
Statement body 2;
break;
...
default:
Statement n;
[break;]
}
case: followed by the value to be compared with the expression;
break; means the end of the terminal, ending the switch statement
default: indicates that when all conditions do not match, the content of this location will be executed, which is similar to else of if statement
case 'Penetration
Composition of cycle structure:
Initialization statement: used to indicate the starting state when the loop is opened. In short, it is what it looks like when the loop starts
Condition judgment statement: used to indicate the condition of repeated execution of the loop. In short, it is used to judge whether the loop can be executed all the time
Loop body statement: used to represent the content of loop repeated execution, which is simply the matter of loop repeated execution
Conditional control statement: used to represent the contents of each change in the execution of the loop. In short, it controls whether the loop can be executed
for loop statement format
for (initialization statement; conditional judgment statement; conditional control statement){
Loop body statement;
}
Execution process
Initialization statement - > conditional judgment statement -- > loop body statement -- > conditional control statement -- > conditional judgment statement -- > loop body statement
public class Project2 { public static void main(String[] args) { for (int i = 1;i<=10;i++) { System.out.println("i:"+i); } for (int i = 1; i<=10;++i){ System.out.println("i:"+i); } } }
F:\>javac Project2.java F:\>java Project2 i:1 i:2 i:3 i:4 i:5 i:6 i:7 i:8 i:9 i:10 i:1 i:2 i:3 i:4 i:5 i:6 i:7 i:8 i:9 i:10
How to find the value in the specified bit of any number
First move the required number to the single digit with the integer division operation, and then use the remainder operation to get the value in the last digit
How many digits are there after this bit Divide by 10 to the power of 10 and take the remainder of 10
while loop statement
Full format:
Initialization statement;
while (conditional judgment statement){
Loop body statement;
Conditional control statement;
}
public class Project2 { public static void main(String[] args) { double paper = 0.1; int i=2; int j=0; int zf=8844430; while (paper<=zf) { j++; paper *=i; } System.out.println("Fold times"+j); } }
F:\>javac Project2.java F:\>java Project2 The number of folds is 27
do...while loop statement
Full format:
Initialization statement;
do{
Loop body prediction; conditional control statement;
}while (conditional judgment statement);
It is judged that the loop will continue to be executed
Dead cycle
for(;;){
}
while(true)
{
}
do{
}while(ture);
Jump control statement
Continue is used in a loop. Based on condition control, skip the execution of a loop and continue the next execution,
break Just in the loop, based on condition control, terminate the execution of the loop, that is, end the current whole loop
loop nesting
import java.util.Scanner; public class Project2 { public static void main(String[] args) { System.out.println("Enter hour"); Scanner h = new Scanner(System.in); int i = h.nextInt(); System.out.println("Enter minutes"); Scanner m = new Scanner(System.in); int j = m.nextInt(); for(int hour=i;hour <24;hour++) { for(int minute=i;minute<60;minute++) { int f=0; i=f; System.out.println(hour+":"+minute); } } }
F:\>javac Project2.java F:\>java Project2 Enter hour 10 Enter minutes 20 10:10 10:11 10:12 10:13 10:14 10:15 10:16 10:17 10:18 10:19 10:20 10:21 10:22 10:23 10:24 10:25 10:26 10:27 10:28 10:29 10:30 10:31 10:32 10:33 10:34 10:35 10:36 10:37 10:38 10:39 10:40 10:41 10:42 10:43 10:44 10:45 10:46 10:47 10:48 10:49 10:50 10:51 10:52 10:53 10:54 10:55 10:56 10:57 10:58 10:59 11:0 11:1
The role and steps of random
Function: used to generate a random number
Use steps:
1. Guide Package
import java,uti,Random; (the action of importing packages must appear above the class definition)
2. Create object
Random r = new Random(); (in this format, R is the variable name, which can be changed, and others are not allowed to be changed)
3. Get random number
int number = r.nextInt(10); / / obtain the range of data; [0, 10) includes 0 and does not include 10
(in this format, number is the variable name, which can be changed, number 10 can be changed, and others are not allowed to be changed)
import java.util.Random; public class Project2 { public static void main(String[] args) { Random s = new Random(); for(int i=0;i<10;i++) { int number = s.nextInt(20); System.out.println(number); } } }
F:\>javac Project2.java F:\>java Project2 11 18 16 8 10 14 6 8 7 13