In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
We learn big data technology from scratch, from the foundation of java, to Linux technology, then to the Hadoop, Spark, Storm technology of big data technology, and finally to the construction of big data enterprise platform, step by step, from point to face! I hope Technology Bull can come to guide the study.
In the previous section, you learned about process control statements, and this section learned about switch statements.
Before we begin, let's take a look at the exercises in the previous section.
1. Exercises
Exercises for formatting if statements:
1. Get the larger values of the two data
2. Judge whether a data is odd or even, and output
The execution result of the program is as follows:
II. Switch statement
The selection structure of process control statements
1 Select structure (also known as branch structure)
There are two kinds of statements for the selection structure, which are as follows:
If statement
Switch statement
The if statement has been discussed earlier, so let's take a look at the switch statement in this section.
Switch statement
Switch statement format:
Switch (expression) {
Case value 1:
Sentence aspect 1
Break
Case value 2:
Sentence style 2
Break
...
Default
Sentence style nasty 1
Break
}
Interpretation of the format:
Switch: indicates that this is the switch selection structure
Expression: the value of this place is limited
Byte,short,int,char
JDK5 can be enumerated later.
JDK7 can be a string later.
Case: followed by the value to be compared with the expression
Statement style: code to be executed
Break: means to break and end, and you can control the end of the switch statement.
Default: when all values and expressions do not match, the statement controlled by default is executed, which is actually equivalent to the else of if statement.
Perform the process:
First, calculate the value of the expression and compare it with case in turn.
Once there is a corresponding value, the corresponding statement body is executed.
In the process of execution, it will end when it encounters break
Finally, if all case do not match the value of the expression, the body of the statement in default is executed and the program ends
Think about:
Can byte be used as an expression for switch?
Can long be used as an expression for switch?
Can String be used as an expression for switch?
Example:
The keyboard enters a data, and according to this data, we output the corresponding week
Keyboard input 1, corresponding to output Monday
Keyboard input 2, corresponding to output Tuesday
...
Keyboard input 7, corresponding to output Sunday
Case analysis:
1. Keyboard input, which is realized by switch
2. Judge that we can use either if statement or switch statement
Program code:
Program execution result:
Considerations for switch statements:
1. Case can only be followed by a constant, not a variable, and the values after multiple case cannot have the same
2. Can default be omitted?
It can be omitted, but not recommended, because its function is to give a hint of incorrect situations.
Special case: case can make the value fixed (single option), you can consider omitting default
3. Can break be omitted?
Can be omitted, will not report an error, but the final implementation result is not what we want, there will be case penetration phenomenon.
Omission is not recommended.
4 、
Does default have to be at the end?
No, it can be anywhere, but it is recommended at the end, otherwise sink will generate case penetration.
5 、
End condition of switch statement
It's over when you encounter break.
Switch considerations-Instanc
Let's take a look at the effect of the implementation according to the relevant examples.
1. Example (same name case ID & variable ID)
Program execution result:
It can be seen that case must be followed by a constant, not a variable, and the value after case cannot be the same.
2. Example (omit default statement)
Program execution result:
It can be seen that default can be omitted in the swith statement, but it will eventually lead to some incorrect situations that are not excluded and can be entered normally. Although there is no error in omitting default, omitting it is not recommended.
3. Example (omitting break)
Program execution result:
It can be seen that when the program is executed, if there is no break to interrupt the program, then the program will be executed sequentially until it encounters break. Although omitting break will not make an error, it is not recommended to omit, otherwise the program will print the result incorrectly.
4. Example (the default statement is not placed at the end)
Program execution result:
It can be seen that if default is not placed at the end, the program itself will not report an error, but when it is finally executed, there will be case penetration.
5. Example: (end condition of switch statement)
Program execution result:
It can be seen that in the statement xQuery y, the output of the statement ends directly when it encounters break.
If no break is encountered in the statement A _ break b, the program will not output until the end of execution.
Extended exercises (completed by yourself):
1. Give a random multiple choice question, enter it through the keyboard, and give your choice according to our choice.
2, according to the string entered by the keyboard, judge whether it meets the requirements, and if so, output otherwise, the prompt is wrong.
Let's finish this section first, and in the next section we will learn about loop structure statements.
Previous articles:
Start with big data-Java Foundation from scratch (1)
Learning big data-Java basic Grammar from scratch (2)
Learn big data-Java operator from scratch (3)
Learn big data-Java Basics-Ternary operator / Keyboard entry from scratch (4)
Learn big data-Java fundamentals-process control statements from scratch (5)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.