Java Basic switch statement

Source: Internet
Author: User

Switch statement is a single conditional multi-branch switch statement, its general format is defined as follows ( where The break statement is optional):

Switch ( expression )

{

Case constant Value :

a number of statements

Break

Case constant Value :

a number of statements

Break

...

Case constant Value N:

a number of statements

Break

Default

several statements

}

The values for expression and constant values in the switch statement must be byte,short,int, Char type or enum type, and not identical.

Import Java.util.Scanner;

public class demo9{

public static void Main (String args[]) {

Scanner reader = new Scanner (system.in);

SYSTEM.OUT.PRINTLN ("please input int");

int input = Reader.nextint (); /* get input int data

switch (input) {

Case 1:/* input is 1

SYSTEM.OUT.PRINTLN ("You input 1");

Break

Case 2:

SYSTEM.OUT.PRINTLN ("You input 2"); /* input is 2

Break

Default:/* input is neither 1nor 2

SYSTEM.OUT.PRINTLN ("You input Other");

Break

}

}

}

Results

Input 2

Input 1

Enter a different number

original link:http://www.maiziedu.com/wiki/java/switch/

A detailed description of the switch statement for Java Basics

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.