1, Bit operation
class Demo01 { public static void main(String[] args) { int a = 5; int b = 3; /* 0000 0101 |0000 0011 ========== 0000 0111 = 7 */ System.out.println(a | b); /* 0000 0101 &0000 0011 ========== 0000 0001 = 1 */ System.out.println(a & b); /* XOR 0000 0101 ^0000 0011 ========== 0000 0110 */ System.out.println(a ^ b); } }
2, Process control
-
Sequential structure
class Demo02 { public static void main(String[] args) { // Sequential structure, code execution from top to bottom System.out.println("Hello World!1"); System.out.println("Hello World!2"); System.out.println("Hello World!3"); System.out.println("Hello World!5"); System.out.println("Hello World!4"); System.out.println("Hello World!6"); System.out.println("Hello World!7"); System.out.println("Hello World!" + a); int a = 8; } }
-
Branching structure
-
Cyclic structure
3, Branch structure -- if
3.1 grammatical structure
If (judgment condition){
Code executed when the condition is true
}
if Keywords in java
Judgment conditions
boolean values can be written directly
It can also be an expression whose return value is boolean type
{}
Meet the condition -- the code executed when the condition is true
3.2 if
class Demo03 { public static void main(String[] args) { System.out.println("Hello World!"); // Suppose you go through 888 and can only bring 10 yuan. What do you want to bring int year = 88; /* if(Year is 888){ I want to bring some things over } */ if(year == 888){ System.out.println("I hope to bring some hot pot balls..."); } // Determine whether a knife is a controlled tool int knifeLen = 155; if(knifeLen >= 220){ System.out.println("This knife is a controlled knife and should be confiscated..."); } if(knifeLen < 220){ System.out.println("This knife is not a controlled knife and can be carried..."); } } }
3.3 if...else
If (judgment condition){
Code block 1
}else{
Code block 2
}
if is a keyword in java, enabling condition judgment
The judgment condition is the result of a boolean type
Code block 1 is the code executed when the conditions in if are met
Else is a keyword in java. When the if condition is false, execute code block 2 after else
Case 01
class Demo04 { public static void main(String[] args) { System.out.println("Hello World!"); int knifeLen = 150; // if...else if (knifeLen >= 220){ System.out.println("Control tool"); }else{ System.out.println("Uncontrolled tool"); } } }
Case 02
import java.util.Scanner; class Demo05 { public static void main(String[] args) { // The user enters the user name and password to judge whether the login is successful /* 1,Import Scanner 2,Create Scanner object 3,Prompt for user name 4,Get user name 5,Prompt for password 6,Get password 7,Compare user name and password 8,Output results */ int USERNAME = 123456789; int PASSWORD = 987654321; // 2. Create Scanner object Scanner in = new Scanner(System.in); System.out.println("Please enter QQ number:"); int username = in.nextInt(); System.out.println("Please enter QQ Password, only numbers:"); int password = in.nextInt(); // 7. Compare user name and password if((username == USERNAME) && (password == PASSWORD)){ System.out.println(username + ",Welcome."); }else{ System.out.println("Wrong account or password..."); } } }
3.4 if...else if...
If (judgment condition 1){
Code executed when condition 1 is met
}Else if (judgment condition 2){
Code executed when condition 2 is met
}Else if (judgment condition 3){
Code executed when condition 3 is met
}. . . else{
None of the above conditions holds. Execute the code here
}
The whole if.. As long as there is a valid condition in else if judgment, the code in it will be executed. After the code execution, the whole if judgment will end
import java.util.Scanner; class Demo07 { public static void main(String[] args) { /* Enter a number (1-7) on the keyboard and output the course to be attended on the corresponding date 1,Import Scanner 2,Create Scanner object 3,Prompt for date 1-7 4,get date 5,Use if judgment to output the courses to be taken on this day */ Scanner in = new Scanner(System.in); System.out.println("Please enter the course to query the day of the week(1--7)"); int day = in.nextInt(); // 5. Use if judgment to output the courses to be taken on this day if(day == 1){ System.out.println("Today is Monday,Need a Chinese class."); } else if(day == 2){ System.out.println("Today is Tuesday,Need a math class."); } else if(day == 3){ System.out.println("Today is Wednesday,Need English class."); } else if(day == 4){ System.out.println("Today is Thursday,Need physical education."); } else if(day == 5){ System.out.println("Today is Friday,Need a Chinese class."); } else if((day == 6) || (day == 7)){ System.out.println("Today is Sunday" + day + ",Need rest."); } else{ System.out.println("The course you queried does not exist..."); } // Enter a month on the keyboard to judge what season it is // Enter a year on the keyboard to judge whether it is a leap year } }
Enter month to display season
import java.util.Scanner; class Demo08 { public static void main(String[] args) { /* Enter a number (1-7) on the keyboard and output the course to be attended on the corresponding date 1,Import Scanner 2,Create Scanner object 3,Prompt for month 4,Get month 5,Use if judgment to output the season of this month */ Scanner in = new Scanner(System.in); System.out.println("Please enter the month of query(1--12)"); int month = in.nextInt(); // Enter a month on the keyboard to judge what season it is // Use if judgment to output the season of this month if((month==3) || (month==4) || (month==5)){ System.out.println("Spring is coming,Recovery of all things,It's time for everything to grow again..."); } else if((month==6) || (month==7) || (month==8)){ System.out.println("It's summer,Hot weather,Crayfish ha beer is a very good summer product..."); } else if(month>=9 && month<=11){ System.out.println("Autumn is coming,fresh autumn weather,It's the receiving season..."); } else if((month==12) || (month==1) || (month==2)){ System.out.println("Winter is coming,The weather is cold,The winter in Hangzhou will leave a deep impression on you..."); } else{ System.out.println("Month does not exist..."); } } }
3.5 if nesting
Write the if branch statement in if
If (condition 1){
If (condition 2){
Execute code that meets conditions 1 and 2
}else{
Execute code that meets condition 1 but does not meet condition 2
}
}else{
Execute code that does not meet condition 1
}
3.5.1 ride cases
import java.util.Scanner; class Demo10 { public static void main(String[] args) { /* Simulate the process of ticket checking and security inspection by train */ Scanner in = new Scanner(System.in); System.out.println("Please show me your ticket(1=Tickets, 0=No ticket):"); int ticket = in.nextInt(); // Judge whether there is a ticket. If there is, you can go to the security check if(ticket>=1){ // Prompt for security check System.out.println("Are you carrying any prohibited items(true=have false=nothing):"); boolean isDanger = in.nextBoolean(); // Judge the safety situation if(isDanger){ System.out.println("Please follow me...."); }else{ System.out.println("Security check passed,Please wait in line..."); } }else{ // No ticket, prompt to buy a ticket System.out.println("Please buy your ticket first..."); } } }
3.5.2 lucky money cases
import java.util.Scanner; class Demo11 { public static void main(String[] args) { /* Judge what you want to do according to the lucky money */ Scanner in = new Scanner(System.in); System.out.println("Do you have lucky money:"); boolean hasLuckyMoney = in.nextBoolean(); if(hasLuckyMoney){ // When there is lucky money System.out.println("Please enter the amount of lucky money received:"); int luckyMoney = in.nextInt(); // 0-500 to have a big meal, 500-1000 to upgrade the computer configuration, 1000-10000 to buy a bike, more than 10000 to donate if((luckyMoney>0) && (luckyMoney<=500)){ System.out.println("I'm very happy to receive the lucky money,We are going to invite the whole village to a big meal..."); } else if((luckyMoney>500) && (luckyMoney<=1000)){ System.out.println("I'm very happy to receive the lucky money,Upgrade the configuration of the computer..."); } else if((luckyMoney>1000) && (luckyMoney<=10000)){ System.out.println("I'm very happy to receive the lucky money,Buy a bike and ride to Guangming top..."); } else if(luckyMoney>10000){ System.out.println("I'm very happy to receive the lucky money,Give it to people who need it more..."); } }else{ // No money System.out.println("Not receiving the lucky money means you have grown up,You can give others lucky money<_>"); } } }
4, switch of branch statement
- Meaning of change-over and switch
- Match one by one
switch(key){
case value1:
Code executed when key==value1 is satisfied
break;=== Terminate the execution of switch
case value2:
Code executed when key==value2 is satisfied
break;=== Terminate the execution of switch
. . . . . .
default:
Code executed when all the above matches fail
break;=== Terminate the execution of switch
}
4.1 basic use of switch
import java.util.Scanner; class Demo13 { public static void main(String[] args) { /* Output the class content according to the day of the week 1,Import Scanner 2,Create Scanner object 3,Prompt output day of the week 4,Gets the value entered 5,Use switch to judge and output course content */ Scanner in = new Scanner(System.in); System.out.println("Please enter the day of the week today(1--7):"); int day = in.nextInt(); // 5. Use switch to judge and output course content switch(day){ case 1: System.out.println("Today is Monday,Need a Chinese class."); break; case 2: System.out.println("Today is Tuesday,Need a Chinese class."); break; case 3: System.out.println("Today is Wednesday,Need a Chinese class."); break; case 4: System.out.println("Today is Thursday,Need a Chinese class."); break; case 5: System.out.println("Today is Friday,Need a Chinese class."); break; case 6: System.out.println("Today is Saturday,Need a Chinese class."); break; case 7: System.out.println("Today is Sunday,Need a Chinese class."); break; default: System.out.println("This time does not exist..."); break; } } }
4.2 case penetration (when the corresponding case is matched and there is no break under the case, the next case will continue to be found)
import java.util.Scanner; class Demo14 { public static void main(String[] args) { /* Output the class content according to the day of the week 1,Import Scanner 2,Create Scanner object 3,Prompt for month 4,Gets the value entered 5,Use switch to judge the output season content */ Scanner in = new Scanner(System.in); System.out.println("Please enter the month(1--12):"); int month = in.nextInt(); // 5. Use switch to judge the output season content switch(month){ case 1: System.out.println("January is winter,Very cold"); case 2: System.out.println("February is winter,Very cold"); case 3: System.out.println("March is spring,Very cold"); case 4: System.out.println("April is spring,Very cold"); case 5: System.out.println("May is spring,Very cold"); case 6: System.out.println("June is summer,Very hot"); case 7: System.out.println("July is summer,Very hot"); case 8: System.out.println("August is summer,Very hot"); case 9: System.out.println("September is autumn,Very hot"); case 10: System.out.println("October is autumn,Very hot"); case 11: System.out.println("November is autumn,Very hot"); case 12: System.out.println("December is winter,Very hot"); default: System.out.println("Month does not exist"); } } }
4.3 case penetration case
import java.util.Scanner; class Demo14 { public static void main(String[] args) { /* Output the class content according to the day of the week 1,Import Scanner 2,Create Scanner object 3,Prompt for month 4,Gets the value entered 5,Use switch to judge the output season content */ Scanner in = new Scanner(System.in); System.out.println("Please enter the month(1--12):"); int month = in.nextInt(); // 5. Use switch to judge the output season content switch(month){ case 1: case 2: case 12: System.out.println(month + "Month is winter,Very cold"); break; case 3: case 4: case 5: System.out.println(month + "Month is spring,Very cold"); break; case 6: case 7: case 8: System.out.println(month + "Month is summer,Very hot"); break; case 9: case 10: case 11: System.out.println(month + "The month is autumn,Very hot"); break; default: System.out.println("Month does not exist"); break; } } }
5, Compare if and switch
Achievement cases
import java.util.Scanner; class Demo15 { public static void main(String[] args) { /* 0 --59 fail, 60--69 pass 70--79 commonly 80--89 good 90--100 excellent */ Scanner in = new Scanner(System.in); System.out.println("Please enter the test result(0--100):"); int score = in.nextInt(); if((score<0) || (score>100)){ System.out.println("Wrong score"); }else{ // Legal score if(score >= 90){ System.out.println("Excellent performance"); } else if(score >= 80){ System.out.println("Good results"); } else if(score >= 70){ System.out.println("Average performance"); } else if(score >= 60){ System.out.println("Just passed"); } else{ System.out.println("fail,,Experience mixed doubles..."); } } } }
-
The judgment condition in if can be equal to a specific value or a range
- month==3
- score>=90
-
The value after case in switch can only be the value within the range of byte, short, int, char and String
-
switch cannot be used when the range cannot be used for operation, the number range is too large, or the content is uncertain and the situation is complex
-
switch can be used when the value is small and the content is fixed
-
switch is recommended for constant determination
-
6, Local variables (Master)
- Variables defined in methods
- Assignment must be made before use --- there is no default value
- The scope of action is only in the current method
- Variable names cannot be repeated in the same scope
- The lifecycle is from creation to the end of the method
class Demo16 { public static void main(String[] args) { /* Define location in method Assignment must be made before use --- there is no default value The scope of action is only in the current method Variable names cannot be repeated in the same scope The lifecycle is from creation to the end of the method */ int age = 20; System.out.println("age=" + age); // int age = 22; // show(); age = 33; } public static void show() { System.out.println("age=" + age); } }