当前位置:网站首页>If switch branch structure
If switch branch structure
2022-06-25 21:22:00 【vancomycin】
if
grammar : if( Boolean expression ){ // Code block 1 }
Execute the process : Judge Boolean expressions . The result is true, Then execute the code block first 1, Then execute the following code . The result is false, Then skip the code block 1, Execute subsequent code directly .
import java.util.Scanner; /** * Reward according to achievements * If * java Results are greater than 90 also Music scores Greater than 85 * perhaps * java Results are greater than 80 also Dance performance be equal to 100 * * Reward apples 12 bag * @author WHD * */ public class Test3 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println(" Please enter java achievement "); double javaScore = input.nextDouble(); System.out.println(" Please enter your music score "); double musicScore = input.nextDouble(); System.out.println(" Please enter your dance score "); double danceScore = input.nextDouble(); // java Results are greater than 90 also Music scores Greater than 85 // java Results are greater than 80 also Dance performance be equal to 100 if((javaScore > 90 && musicScore > 85) || (javaScore > 80 && danceScore == 100)) { System.out.println(" Reward apples 12 bag "); } System.out.println(" Program end "); } }
2.2 if-else
grammar : if( Boolean expression ){ // Code block 1 }else{ // Code block 2 } Subsequent code ...
Execute the process : Judge Boolean expressions . The result is true, Then execute the code block first 1, Then exit the whole structure , Execute subsequent code . The result is false, Then execute the code block first 2, Then exit the whole structure , Execute subsequent code .
if-else amount to 2 choose 1 A code block must be executed , Then exit the whole if-else structure
package com.qfedu.test3; import java.util.Scanner; /** * if-else choice structure * @author WHD * */ public class Test1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println(" Please enter age "); int age = input.nextInt(); if(age >= 18){ System.out.println(" Grown up "); }else { System.out.println(" A minor "); } System.out.println(" Program end "); } }
2.3 multiple if
grammar : if( Boolean expression 1){ // Code block 1 }else if( Boolean expression 2){ // Code block 2 }else if( Boolean expression 3){ // Code block 3 }else{ // Code block 4 }
Execute the process : expression 1 by true, Then execute the code block 1, Then exit the whole structure .
expression 2 by true, Then execute the code block 2, Then exit the whole structure .
expression 3 by true, Then execute the code block 3, Then exit the whole structure .
All of the above are false, Then execute the code block 4, Then exit the whole structure .
Be careful : Mutually exclusive , There is one for true, Others will not be executed , Applicable to interval judgment .
multiple if
else if Not alone Must be combined with if
else It can't be alone Can combine else if perhaps combination if
multiple if be used for Deal with the case where a value is in a continuous interval
When writing judgment conditions Use ascending or descending order Do not use out of sequence
package com.qfedu.test4; import java.util.Scanner; /** * multiple if * else if Not alone Must be combined with if * else It can't be alone Can combine else if perhaps combination if * * demand : Grade according to grades * 100 branch perfect * 90 More than good * 80 More than good * 70 More than secondary * 60 Above and above pass * 60 Below fail, * * multiple if be used for Deal with the case where a value is in a continuous interval * When writing judgment conditions Use ascending or descending order Do not use out of sequence * @author WHD * */ public class Test1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println(" Please enter your score "); int score = input.nextInt(); if(score == 100) { System.out.println(" perfect "); }else if(score > 90) { System.out.println(" good "); }else if(score > 80) { System.out.println(" good "); }else if(score > 70) { System.out.println(" secondary "); }else if(score >= 60) { System.out.println(" pass "); }else { System.out.println(" fail, , come on. ~"); } System.out.println(" Program end "); } }
2.4 nesting if
if( Outer expression ){ if( Inner expression ){ // Inner code block 1 }else{ // Inner code block 2 } }else{ // Outer code block }
Execute the process : When the outer conditions are met , Then judge the inner conditions .
Be careful : In a selection structure , Another selection structure can be nested . When the nesting format is correct , Support any combination .
package com.qfedu.test5; import java.util.Scanner; /** * nesting if A complete if One or more... Are nested in the structure if structure * The grammatical format is correct Can be nested arbitrarily No layers ceiling The actual development shall not exceed three layers * * * demand : * The school held a 100 meter race , If the time is less than 15 second , Can enter the finals * * Men are divided according to sex perhaps Women's group * @author WHD * */ public class Test1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println(" Please enter your running time "); double time = input.nextDouble(); if(time < 15) { // Can enter the finals grouping System.out.println(" Congratulations on entering the finals , Start grouping "); // adopt sex.eqauls(" Content ") Said judgment sex Whether the value of the variable is the same as that in the brackets // For the same true Otherwise false System.out.println(" Please enter gender "); String sex = input.next(); if(sex.equals(" male ")) { System.out.println(" Enter the men's group "); }else if(sex.equals(" Woman ")){ System.out.println(" Into the women's group "); }else { System.out.println(" The third sex is not supported for the time being , Please wait for the next "); } }else { // Come on next time Make persistent efforts System.out.println(" Come on next time !"); } System.out.println(" Program end "); } }
3. Branching structure
3.1 switch
grammar : switch( Variable | expression ){ case value 1: Logic code 1; case value 2: Logic code 2; case value n: Logic code n; default: Logic code when not satisfied ; }
Execute the process : If the value in the variable is equal to the value 1, Execute the logic code 1. If the value in the variable is equal to the value 2, Execute the logic code 2. If the value in the variable is equal to the value n, Execute the logic code n. If the value in the variable does not match case When the value of , perform default Logical code in .
package com.qfedu.test6; import java.util.Scanner; /** * switch Branching structure * demand : Reward according to the ranking * The first name : Reward summer camp activities * proxime accessit : Reward apples 12 bag * The third : Reward a notebook * A fourth : Oral praise once * other : Continue to work hard * @author WHD * */ public class Test1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println(" Please enter your rank "); int num = input.nextInt(); switch(num) { case 1: System.out.println(" Summer camp "); break; case 2: System.out.println(" Apple 12 bag "); break; case 3: System.out.println(" A notebook "); break; case 4: System.out.println(" Oral praise once "); break; default: System.out.println(" Continue refueling ~"); break; } System.out.println(" Program end "); } }
3.2 Supported data types
byte short int char String(JDK7+) enumeration
package com.qfedu.test6; import java.util.Scanner; /** * switch Branching structure * Supported data types * @author WHD * */ public class Test3 { public static void main(String[] args) { char ch1 = 'B'; String str = "D"; switch(str) { case "A": System.out.println(" Summer camp "); break; case "B": System.out.println(" Apple 12 bag "); break; case "C": System.out.println(" A notebook "); break; case "D": System.out.println(" Oral praise once "); break; default: System.out.println(" Continue refueling ~"); break; } System.out.println(" Program end "); } }
4. local variable
characteristic | describe |
---|---|
Scope of action | Within the nearest brace to the current variable |
Define the location | In the method |
The problem of duplicate names | The overlapping scope cannot have the same name |
Storage location ( understand ) | There is a stack of basic data types (stack) in , Reference data type , The name is on the stack (stack), The contents are piled up (heap) |
Life cycle ( understand ) | As the method is stacked ( Pressing stack ) And take effect , As the method goes out of the stack ( Bomb stack ) And death |
边栏推荐
- Cvpr2020 | the latest cvpr2020 papers are the first to see, with all download links attached!
- The correct way to clear the cache of the computer. The computer will not get stuck immediately after changing. practical
- What is a server? (Powercert animated videos)
- Set eye color
- ThreadLocal class
- Is Galaxy Securities reliable? Is it safe to open a securities account?
- [machine learning] machine learning from zero to mastery -- teach you to recognize handwritten digits using KNN
- Jmeter- (III) create user test cases for interface testing
- Illustrated with pictures and texts, 700 pages of machine learning notes are popular! Worth learning
- Multi database and multi table backup and restore of MySQL under Linux
猜你喜欢
Jmeter- (III) create user test cases for interface testing
Mutual conversion of CString and char*
Illustrated with pictures and texts, 700 pages of machine learning notes are popular! Worth learning
Local Yum source production
Php7.4 arm environment compilation and installation error invalid 'ASM': invalid operate prefix '%c'
STM32 self balancing robot project, with code, circuit diagram and other data attached at the end (learning materials and learning group at the end)
[deep learning series] - visual interpretation of neural network
Unable to connect to the server remotely locally using the Jupiter notebook
Xshell mouse configuration
Explain memcached principle in detail
随机推荐
Writing manuals using markdown
Renren mall locates the file according to the route
Command 'GCC' failed with exit status 1 when PIP install mysqlclient
Bank digital transformation layout in the beginning of the year, 6 challenges faced by financial level structure and Countermeasures
What is ARP (address resolution protocol)? (Powercert animated videos)
Decryption of APP parameters of a cross-border export e-commerce - dunhuang.com
Working principle and experimental analysis of DHCP
Local Yum source production
Get parameters in URL
Analysis and cleaning of kdevtmpfsi virus content
Finger collar pin exclusive Medal
The robotframework executes CMD commands and bat scripts
[nailing - scenario capability package] nailer card
Basic process of configuring utf8 in idea
Canoe learning notes (1)
Rounding related calculation
Support JPEG format in GD Library in php7.4
启牛证券开户安全嘛?
OBD Position Overview
Jingxi Pinpin wechat applet -signstr parameter encryption