Java Personal Learning Note: switch

Source: Internet
Author: User

The return value of an expression in switch is one of the byte short char int enumeration strings.

The value in the case must be a constant, and the value in the case clause is different.

Default is optional, when no matching value in case jumps to default

Break jumps out of a switch loop statement after executing a case statement. Without break, the program executes to the end of the switch. (In other words, if a case is executed, there is no definition Break,switch will execute all of the rest of the following or the default statement)

Scanner Scanner = new Scanner (system.in);
System.out.println ("Enter year:");
int year = Scanner.nextint ();
System.out.println ("Enter month:");
int month = Scanner.nextint ();
System.out.println ("Enter day:");
int day = Scanner.nextint ();
int sumday = 0;
Switch (month) {
Case 12:
Sumday + = 30;
Case 11:
Sumday + = 31;
Case 10:
Sumday + = 30;
Case 9:
Sumday + = 31;
Case 8:
Sumday + = 31;
Case 7:
Sumday + = 30;
Case 6:
Sumday + = 31;
Case 5:
Sumday + = 30;
Case 4:
Sumday + = 31;
Case 3:
if (year% 4 = = 0 && year%!! = 0 | | year% 400 = = 0)
Sumday + = 29;
Else
Sumday + = 28;
Case 2:
Sumday + = 31;
Case 1:
Sumday + = day;

}
SYSTEM.OUT.PRINTLN (Year + "years" + month + "Month" + Day + "date is this year's first" + Sumday
+ "Day");

Above for example you entered May, the code will go into case 5: Then the code does not have break,case behind the code will be executed.

Java Personal Learning Note: 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.