-
content
Basic syntax of switch statement
-
Why use switch statement
- The switch statement provides a workaround for the if statement that selects one of several blocks of statements to execute. The switch statement is Java's multi-way branch statement. It provides an easy way to make different parts of a program execute based on the value of an expression.
-
Basic syntax of switch statement
- 1.switch keyword, which means switch branch.
- 2. An expression corresponds to a value.
- 3.case constant 1: When the value of the expression is equal to the constant 1, the statement block 1 is executed.
- 4.break: means to exit the switch.
- 5. If it matches case constant 1, execute statement block 1, if not, match case constant 2.
- 6. If none of them match, execute default.
-
Notes on switch statements
- 1. Cases can only be followed by constants, not variables, and the values behind multiple cases cannot appear the same.
- 2. defalut can be omitted but it is generally not recommended to omit.
- 3.break can be omitted, but the running result may not be what we want.
- 4.default can appear anywhere in the switch statement.
- 5. The end condition of the switch statement:
- Execute to the end of the program
- encounter break statement
-
take a chestnut
-
package demo02; import java.util.Scanner; public class demo1 { public static void main(String[] args) { /* 编写一个程序,这个程序可以接收一个数字,比如1,2,3,4,5,6,7 1表示星期一,2表示星期二,以此类推 */ Scanner myscanner = new Scanner(System.in); System.out.println("请输入一个数字:"); int week = myscanner.nextInt(); switch (week) { case 1: System.out.println("星期一"); break; case 2: System.out.println("星期二"); break; case 3: System.out.println("星期三"); break; case 4: System.out.println("星期四"); break; case 5: System.out.println("星期五"); break; case 6: System.out.println("星期六"); break; case 7: System.out.println("星期日"); break; default://如果以上的都没有匹配到就输出default语句 System.out.println("输入的数字不正确......"); break; } } }
-
switch triple tap
- 1. Among the following options, which data types can the switch statement judge the condition receive? (ABCD)
- A.int
- B.byte
- C.char
- D.short
- Summary: Java's switch is special, switch only accepts data of int type, then if it receives a low type, it will be turned up to int.
- 2. The following statement about if statement and switch statement is wrong: (D)
- A. Both the if statement and the switch statement can implement the selection structure of the algorithm.
- B. Both the if statement and the switch statement can implement multiplexing.
- C.if statements can be nested.
- D.switch statements cannot be nested.
- 3. In the following switch statement, what types of data can x be: (BDF)
- A.long
- B.char
- C.float
- D.byte
- E.double
- F.String
- Explanation: According to the Java 8-bit standard, switch supports 10 types
- Basic type: byte char short int
- For wrapper class: Byte Short Character lnteger String enum
[Add Brick Java] When my classmates went to the toilet between classes, I learned the switch statement.
Guess you like
Origin blog.csdn.net/m0_62069409/article/details/124369095
Recommended
Ranking