switch && if...else

/*导包
    1:手动导包:import java.util.Scanner;
    2:快捷键:alt+enter
    3:自动导包:Scann+回车
 */


//switch 循环

import java.util.Scanner;

public class Test02 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入一个星期数: ");
        int week = sc.nextInt();

        switch (week) {
            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;
            default:
                System.out.println("你输入的星期数有误。");

        }
    }
}


//if...else 语句
import java.util.Scanner;

public class Test01 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个星期数: ");
        int week = sc.nextInt();

        if (week < 1 || week > 7) {
            System.out.println("你输入的星期数有误");
        } else if (week == 1) {
            System.out.println("跑步");
        } else if (week == 2) {
            System.out.println("游泳");
        } else if (week == 3) {
            System.out.println("慢走");
        } else if (week == 4) {
            System.out.println("动感单车");
        } else if (week == 5) {
            System.out.println("拳击");
        } else if (week == 6) {
            System.out.println("爬山");
        } else {
            System.out.println("好好吃一顿");
        }
    }
}

  

switch && if...else

上一篇:Robot Framework 的安装和配置(转载)


下一篇:srvctl 命令