Java 基本语法 switch-case 例题练习

目录

Java 基本语法 switch-case 例题练习

练习一

class Test {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("请输入一个小写字母:");
        String str = scan.next();
        char c = str.charAt(0);
        switch (c) {
            case 'a':
                System.out.print("A");
                break;
            case 'b':
                System.out.print("B");
                break;
            case 'c':
                System.out.print("C");
                break;
            case 'd':
                System.out.print("D");
                break;
            case 'e':
                System.out.print("E");
                break;
            default:
                System.out.print("other");
                break;
        }
    }
}

结果
Java 基本语法 switch-case 例题练习
Java 基本语法 switch-case 例题练习
Java 基本语法 switch-case 例题练习

练习二

Java 基本语法 switch-case 例题练习
这样如此肯定不合理

package hh;
import java.util.Scanner;

public class test02 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("请输入你的成绩:");
        int score = scan.nextInt();
        if(score > 60){
            System.out.println("合格");
        }else
            System.out.println("不合格");
    }
}

结果
Java 基本语法 switch-case 例题练习
程序

package hh;
import java.util.Scanner;

public class test02 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("请输入你的成绩:");
        int score = scan.nextInt();
        int scoredemo = score/10;
        switch(scoredemo){
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
                System.out.println("不合格");
                break;
            case 6:
            case 7:
            case 8:
            case 9:
            case 10:
                System.out.println("合格");
                break;
        }
    }
}

结果
Java 基本语法 switch-case 例题练习
Java 基本语法 switch-case 例题练习

练习三

package hh;
import java.util.Scanner;

public class test02 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入2019年月份");
        int month = scan.nextInt();
        System.out.println("请输入2019年日期");
        int day = scan.nextInt();
        int sumDays = 0;
        switch (month) {
            case 12:
                sumDays += 30;
            case 11:
                sumDays += 31;
            case 10:
                sumDays += 30;
            case 9:
                sumDays += 31;
            case 8:
                sumDays += 31;
            case 7:
                sumDays += 30;
            case 6:
                sumDays += 31;
            case 5:
                sumDays += 30;
            case 4:
                sumDays += 31;
            case 3:
                sumDays += 28;
            case 2:
                sumDays += 31;
            case 1:
                sumDays += day;
        }
        System.out.println(sumDays+"天");
    }
}


结果
Java 基本语法 switch-case 例题练习
倒着从12月份开始加天数,这样子更简单。

加练习

package hh;
import java.util.Scanner;

public class test02 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入年份");
        int year = scan.nextInt();
        System.out.println("请输入月份");
        int month = scan.nextInt();
        System.out.println("请输入几号");
        int day = scan.nextInt();
        int sumDays = 0;
        switch (month) {
            case 12:
                sumDays += 30;
            case 11:
                sumDays += 31;
            case 10:
                sumDays += 30;
            case 9:
                sumDays += 31;
            case 8:
                sumDays += 31;
            case 7:
                sumDays += 30;
            case 6:
                sumDays += 31;
            case 5:
                sumDays += 30;
            case 4:
                sumDays += 31;
            case 3:
                //判断是否是闰年
                if((year % 4 == 0 && year % 100 != 0)|| year % 400 == 0){
                    sumDays += 29;
                }else
                    sumDays += 28;
            case 2:
                sumDays += 31;
            case 1:
                sumDays += day;
        }
        System.out.println(year + "年" + month + "月" + day + "日是当年的第" + sumDays+ "天" );
    }
}


结果
Java 基本语法 switch-case 例题练习

上一篇:你还在用分页?试试 MyBatis 流式查询,真心强大!


下一篇:Java流程控制 上