Java Note 9 switch

Source: Internet
Author: User


switch statement considerations:
A:case can only be constants, cannot be variables, and values after multiple case cannot appear the same
Can B:default be omitted?
It can be omitted, but it is not recommended, because it is used to prompt for incorrect conditions.
Special cases:
case, you can fix the value.
A,b,c,d
Can c:break be omitted?
can be omitted, but the result may not be what we want.
There is a phenomenon: case penetrating.
In the end, we recommend not omitting
D:default must be at the end?
No, it can be anywhere. But the suggestion at the end.
The end condition of the E:switch statement
A: It's over when you hit the break.
B: Execution ends at the end.

1 ImportJava.util.Scanner;2 3 classSwitchDemo2 {4      Public Static voidMain (string[] args) {5         //Creating a Keyboard entry object6Scanner sc =NewScanner (system.in);7         8         //control keyboard input data9System.out.println ("Please enter a data (1-7):");Ten         intWeek = Sc.nextint ();//3 One          A         //Defining Constants -         //int number = 3; -         //then change the value of the case to number, and you will get an error. the          -         //Switch Judgment Statement -         Switch(week) { -              Case1: +System.out.println ("Monday")); -                  Break; +              Case2: ASystem.out.println ("Tuesday")); at                  Break; -              Case3: -System.out.println ("Wednesday")); -                  Break; -              Case4: -System.out.println ("Thursday")); in                  Break; -              Case5: toSystem.out.println ("Friday")); +                  Break; -              Case6: theSystem.out.println ("Saturday")); *                  Break; $              Case7:Panax NotoginsengSystem.out.println ("Sunday")); -                  Break; the             default: +SYSTEM.OUT.PRINTLN ("The data you entered is wrong"); A                 //Break ; the         } +     } -}

Java Note 9 switch

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.