当前位置:网站首页>Nested if selection structure and switch selection structure
Nested if selection structure and switch selection structure
2022-07-25 04:01:00 【Raise your eyes and look far away】
nesting if Selection structure
nesting if structure : In a if The selection structure contains a complete if Selection structure
The law of execution : Execute the corresponding code according to whether the condition is true
The acquisition of random numbers :
Math.random(): Randomly generate a greater than or equal to 0.0 Less than 1.0 Of double Data of type
Math.random()*10: Randomly generate a greater than or equal to 0.0 Less than 10.0 Of double Data of type
(int)(Math.random()*10): Randomly generate a greater than or equal to 0 Less than 10 Of int Data of type
summary : Pick one at random [num1,num2) Between int Type integer (num2>num1)
int num =(int)(Math.random()*(num2-num1)+num1);
eg:
// Randomly generate a greater than or equal to 19, Less than 51 Between int Number of types
int num4 = (int) (Math.random() * 32 + 19);
System.out.println(num4);
switch Selection structure
switch choice :
Rule of grammar :
switch ( expression ){
case Constant 1:
// sentence ;
break;
case Constant 2:
// sentence ;
break;
case Constant 3:
// sentence ;
break;
......
case Constant n:
// sentence ;
break;
default:
// sentence ;
break;
}
The law of execution :
1) Match according to the value of the expression case The following constant values ,
After matching, execute the corresponding statement , Finish executing the following statement , perform break,
End the whole thing switch structure , So let's keep going switch Select the code after the structure
2) If the value of the expression does not match the value of all constants ,
Is executed default The words in it ,
Execute... After execution break Quit the whole switch Selection structure
Be careful :
1) The value of an expression can be int、short、byte、char、 Enumeration type 、String(JDK1.7 Later support )
2)case The following constant values cannot be the same
3)default Sentences can be left blank . If you write that the value of the expression does not match all constant values ,
Is executed default The sentence in .default Statements can be written anywhere , Generally written in the book switch Last bit of Set up .
4)switch Choosing a structure can only deal with the case of equivalent judgment
5)break The function of 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 " through The phenomenon ".
case Penetration phenomenon can be used reasonably
Penetration phenomenon eg:
/*
* Enter month , Output season
*
* 12 1 2 In the winter 3 4 5 In the spring 6 7 8 In the summer 9 10 11 In the fall
*/
int month=4;
switch(month){
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;
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;effect :
边栏推荐
- [Flink] transform operator map
- Shell string
- Interview question 08.07. Permutation and combination of non repeated strings DFS method
- 292. Nim game
- Openharmony Mengxin contribution Guide
- What should testers do if they encounter a bug that is difficult to reproduce?
- 考研经历
- 300. Longest increasing subsequence
- Operations in shell
- Bond network card mode setting
猜你喜欢

Skywalking distributed link tracking, related graphics, DLJD, cat

Force deduction brush question 14. Longest common prefix

Multithreading advanced Step2

Acwing 870. approximate number

DNS resolution experiment

Chapter 3 business function development (modify the remarks of market activities)

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

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

Deep learning Titanic (beginner) kaggle Liu er's homework Lesson 8

ECCV 2022 | rethinking image blending for data enhancement in vision transformers
随机推荐
Debezium series: Show slave status view the possible different situations of master-slave delay
C language file operation
场景之在线人数或者粉丝查询实现
A code takes you to draw multi format sangjimei pictures such as interactive +pdf+png
Cluster clock synchronization configuration
Machine learning exercise 8 - anomaly detection and recommendation system (collaborative filtering)
[Flink] transform operator map
Huawei od maximum consumption JS
JS absolute minimum value of the sum of Huawei od two numbers
Openharmony Mengxin contribution Guide
基于ABP实现DDD--领域逻辑和应用逻辑
Shell string
Skywalking distributed link tracking, related graphics, DLJD, cat
51 single chip microcomputer measures the period of square wave signal
Optimize bubble sorting
Wechat applet authorized login (including obtaining basic information and binding mobile number)
MySQL
Force the resumption of game 302 of the week
Deeply understand the connection state and reliable mechanism of TCP protocol
Modbus poll/slave simulator tutorial