search
HomeJavajavaTutorialHow to use switch statement in Java and examples

Explanation

1. In the switch statement, the variable type can be: byte, short, int or char. Since JavaSE7, switch supports the String type, and case labels must be string constants or literals.

2. The switch sentence can have multiple case sentences. Each case is followed by a value and a colon.

3. The switch sentence can contain a default branch, which is usually the last branch of the switch sentence (can be at any position, but usually at the last one). When default is executed, the value of no case statement is equal to the variable value. The default branch does not require a break statement.

Example

Scanner in = new Scanner(System.in);
System.out.println("请输入当前月份");
int month = in.nextInt();
switch (month){
    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;
    case 8 :
        System.out.println("八月");break;
    case 9 :
        System.out.println("九月");break;
    case 10 :
        System.out.println("十月");break;
    case 11 :
        System.out.println("十一月");break;
    case 12 :
        System.out.println("十二月");break;
    default:
        System.out.println("错误的输入");break;
}

The above is the detailed content of How to use switch statement in Java and examples. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.