The following is a simple method of Switch:
Switch (){
Case B;} the value in Section A must be of the int type, or can be automatically converted to an int type expression. That is to say, Part A can be of the byte/short/Char/INT type. It should be emphasized that the value of Part B in the program must be a single byte/short/Char/INT type value or a final type variable. However, final variables also have requirements, that is, they must be constants during compilation. For more information, see the following program section: Final int A = 0; Final int B; the second statement is a variable that cannot be recognized during compilation because it is not initialized. Of course, this statement is also incorrect. Therefore, the value after case can be a constant value or a final value.
Let's look at the following section:
Public class testswitch {public static void main (string [] ARGs) {byte A = 11; Switch (a) {// cCase 11: system. out. println ("11"); break; case225: system. out. println ("11"); break; // d }}}
Is the code correct? The answer is no. Although it is valid in C, that is, the value of the byte type can appear in the switch, but the statement at D, that is, the value after the second case, is 225, which exceeds the byte range, so it is incorrect. The value after case cannot be repeated. Therefore, pay attention to it.
= Write Break; statement. So what are the consequences? The following small program section will tell you: public class testswitchcase {public static void main (string [] ARGs) {byte A = 2; Switch () {Case 1: system. out. println ("A"); Case 2: system. out. println ("B"); Case 3: system. out. println ("C"); Case 4: system. out. println ("D"); default: system. out. println ("default ");
}}}========= The output result is: B c d default ---------------------------- have you seen it? Even the default command is executed. Pay attention to the end character break; then OK.
It seems that the syntax is simple, but the simpler it is, the easier it is to ignore it. Once an error occurs in a large project, it is hard to find out what is wrong, and then ....... haha. The following is a simple method of Switch:
Switch (){
Case B;} the value in Section A must be of the int type, or can be automatically converted to an int type expression. That is to say, Part A can be of the byte/short/Char/INT type. It should be emphasized that the value of Part B in the program must be a single byte/short/Char/INT type value or a final type variable. However, final variables also have requirements, that is, they must be constants during compilation. For more information, see the following program section: Final int A = 0; Final int B; the second statement is a variable that cannot be recognized during compilation because it is not initialized. Of course, this statement is also incorrect. Therefore, the value after case can be a constant value or a final value.
Let's look at the following section:
Public class testswitch {public static void main (string [] ARGs) {byte A = 11; Switch (a) {// cCase 11: system. out. println ("11"); break; case225: system. out. println ("11"); break; // d }}}
Is the code correct? The answer is no. Although it is valid in C, that is, the value of the byte type can appear in the switch, but the statement at D, that is, the value after the second case, is 225, which exceeds the byte range, so it is incorrect. The value after case cannot be repeated. Therefore, pay attention to it.
= Write Break; statement. So what are the consequences? The following small program section will tell you: public class testswitchcase {public static void main (string [] ARGs) {byte A = 2; Switch () {Case 1: system. out. println ("A"); Case 2: system. out. println ("B"); Case 3: system. out. println ("C"); Case 4: system. out. println ("D"); default: system. out. println ("default ");
}}}========= The output result is: B c d default ---------------------------- have you seen it? Even the default command is executed. Pay attention to the end character break; then OK.
It seems that the syntax is simple, but the simpler it is, the easier it is to ignore it. Once an error occurs in a large project, it is hard to find out what is wrong, and then ....... haha.