当前位置:网站首页>Day008 select structure (switch statement)
Day008 select structure (switch statement)
2022-07-25 04:01:00 【Mo Nian】
switch Selection structure :
Grammatical structure :
switch( expression ){
case Constant 1:
// sentence 1
break;
case Constant 2:
// sentence 2
break;
case Constant 3:
// sentence 3
break;
..........
case Constant n:
// sentence n
break;
default:
// sentence m;
break;
}
The law of execution :
Match according to the value of the expression case The following constant values , After matching, execute the corresponding statement , After executing the corresponding statement , perform break, End the whole thing switch structure , So let's keep going switch Select the code after the structure
If the value of the expression does not match all constant values , Is executed default The words in it , Execute... After execution break Quit the whole switch Selection structure
Be careful :
1) The value of the expression can make int、short、byte、char、 Enumeration type 、String(JDK1.7 And later versions support )
2)case The following constant values cannot be the same
3)default Statement can be omitted . If you write that the value of the expression does not match the value of all expressions , Is executed default The sentence in .default Statements can be written anywhere , Generally written in the book switch At the end of
4)switch Choosing a structure can only deal with the problem of equivalence judgment
5)break The function of the statement is to end the whole switch Selection structure , If there is a lack of , The code will continue to run down , Carry out the next case The sentence in , This phenomenon is called "case Penetration phenomenon ".case Penetration phenomenon can be used reasonably
Example
Use switch Select a structure to judge the day of the year ?
package cn.bdqn.Demo03;
import java.util.Scanner;
public class practice111 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(" Please enter the year :");
int year = sc.nextInt();
//System.out.println(" The year is :");
System.out.println(" Please enter the month :");
int month = sc.nextInt();
//System.out.println(" Month is :");
System.out.println(" Please enter the number of days :");
int day = sc.nextInt();
//System.out.println(" Days are :");
int sumDay = 0;
// Use penetration , Give Way 12 The month is at the top , Down in turn , Avoid penetrating to the end
switch (month) {
case 12:
sumDay += 30;
case 11:
sumDay += 31;
case 10:
sumDay += 30;
case 9:
sumDay += 31;
case 8:
sumDay += 31;
case 7:
sumDay += 30;
case 6:
sumDay += 31;
case 5:
sumDay += 30;
case 4:
sumDay += 31;
case 3:
// Runnian can be divided by four , It cannot be divided by onehundred or fourhundred
if(year%4==0&&year%100!=0||year%400==0){
sumDay += 29;
}else{
sumDay += 28;
}
case 2:
sumDay += 31;
case 1:
// give an example : if 6 month 6 Japan , What is added in the following formula is 6, The added whole month has 1,2,3,4,5 five months , Only added in June 6 God
sumDay += day;
//default:
//System.out.println(" The format of the entered date is incorrect ");
//break;
}
System.out.println(year+" year "+month+" month "+day+" Japan "+" It's the third day of the year "+sumDay+" God ");
System.out.println(" This is the end of program execution ");
sc.close();
}
}


Use switch Select the structure to judge which season of the year a month belongs to .( spring, summer, autumn and winter )
package cn.bdqn.Demo03;
public class practice22 {
public static void main(String[] args) {
int month = 9;
// Use penetration
switch (month) {
case 3:
// System.out.println(" In the spring ");
// break;
case 4:
// System.out.println(" In the spring ");
// break;
case 5:
System.out.println(" In the spring ");
break;
case 6:
// System.out.println(" In the summer ");
// break;
case 7:
// System.out.println(" In the summer ");
// break;
case 8:
System.out.println(" In the summer ");
break;
case 9:
// System.out.println(" In the fall ");
// break;
case 10:
// System.out.println(" In the fall ");
// break;
case 11:
System.out.println(" In the fall ");
break;
case 12:
// System.out.println(" In the winter ");
// break;
case 1:
// System.out.println(" In the winter ");
// break;
case 2:
System.out.println(" In the winter ");
break;
default:
System.out.println(" Month input error ");
break;
}
System.out.println(" The program is finished ");
}
}

边栏推荐
- Unity: text input box for numerical judgment
- Moveit2 - 6. Planning scene monitor
- Implementing DDD based on ABP -- domain logic and application logic
- 基于SSH婴幼儿产品销售系统
- How should enterprise users choose aiops or APM?
- [file upload] parse text files and store them in batches through JDBC connection (dynamic table creation and dynamic storage)
- [kaggle] how to effectively avoid oom and the long process of alchemy
- Network security - information hiding - use steganography to prevent sensitive data from being stolen
- Max matrix and JS
- "Introduction to interface testing" punch in to learn day04: how to abstract the procedural test script into a test framework?
猜你喜欢

Force deduction brush question 26. Delete duplicates in the ordered array
![[laser principle and application -5]: laser diode LD (laser diode) and laser diode driver (LD driver)](/img/1f/4e8ea92a93ed16d8e7944d4a5d22c9.png)
[laser principle and application -5]: laser diode LD (laser diode) and laser diode driver (LD driver)

Network security - information hiding - use steganography to prevent sensitive data from being stolen

Multithreading advanced Step2

Array the same value of key and merge the value value (collation)

Creativity: Modern Art anonymous oil painting AI works presentation

Digital collections can go further without hype

Memory leak due to improper handling of custom view

NC | progress has been made in the study of the ecological network relationship between dissolved organic carbon and microorganisms in the context of global change

P100 MSSQL database penetration test of secondary vocational network security skills competition
随机推荐
One question per day
基于ABP实现DDD--领域逻辑和应用逻辑
An article takes you quickly to learn flex layout
Imeta | ggclusternet microbial network analysis and visualization nanny level tutorial
A code takes you to draw multi format sangjimei pictures such as interactive +pdf+png
Solve "nothing added to commit but untracked files present"“
NC | progress has been made in the study of the ecological network relationship between dissolved organic carbon and microorganisms in the context of global change
DNS domain name resolution service
Yuntu says digital asset chain: your God of digital asset property protection
MIM command
C language: Escape Character
Machine learning exercise 8 - anomaly detection and recommendation system (collaborative filtering)
The latest Matlab download and installation tutorial (with files)
[Flink] rich function
Es- retrieve the selected field from the search
It took me 2 years from Foxconn assembly line to Tencent software testing post~
[Flink] transform operator flatmap
ECCV 2022 | rethinking image blending for data enhancement in vision transformers
Debezium series: in depth interpretation of important JMX indicators of debezium
Leetcode.745. prefix and suffix search____ Double dictionary tree + double pointer