JAVA今日2021.8.5

JAVA Switch

Switch格式

switch(expression){
    case value:
        //语句
        break;//可选
    case value:
        //语句
        break;//可选
    default://可选
        //语句
}

Switch01

package JAVASE.struct;

import java.io.InputStream;

public class switch01 {
    public static void main(String[] args) {
        //switch是匹配一个具体的值     case:穿透
        char grade = 'B';
        switch (grade) {
            case 'A':
                System.out.println("优秀");
                break;
            case 'B':
                System.out.println("良好");
                break;
            case 'C':
                System.out.println("及格");
                break;
        }
    }
}

Switch02

package JAVASE.struct;

import java.util.Scanner;

public class switch02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入成绩(A,B,C):");
        String grade = scanner.nextLine();
        switch (grade){
            case "A":
                System.out.println("优秀");
                break;
            case "B":
                System.out.println("良好");
                break;
            case "C":
                System.out.println("及格");
                break;
        }
    }
}

2021.8.5

上一篇:系统整理Mysql学习笔记——基础命令DQL


下一篇:第一章 排序算法刷题笔记