天天看点

if语句,switch语句复习

案例

import java.util.Scanner;

public class Test01 {
    public static void main(String[] args) {
        //键盘录入一个星期数,通榆变量接收
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个星期数: ");
        int week = scanner.nextInt();
        //对星期数进行判断,这里用if语句实现
        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 if(week==7){
            System.out.println("吃大餐");
        }
    }
}      
import java.util.Scanner;

public class Test02 {
    public static void main(String[] args) {
        //键盘录入一个星期数,通榆变量接收
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个星期数: ");
        int week = scanner.nextInt();
        //对星期数进行判断,用Switch语句实现
        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("你输入的星期数有误");
        }
    }
}