11.10 C++ 笔记+作业

60 阅读1分钟
#include <stdio.h>

int main(){
	//printf("sitch");
	//考试分数 
	int score = 80;
	//case 的值要与 表达式的值完全一样, 才是匹配成功!
	//if(score >= 60 && score <=70) 
	
	
	switch(score){
		case 60:
			printf("额外做1张试卷");
			break;
		case 70:
			printf("额外做半张试卷");
			break;
		case 90:
			printf("出去玩");
			break;
		default:
		printf("你考的分数不在范围");
		break; 
	} 
}

image.png

运行如下:

image.png

#include <stdio.h>

int main(){
	int day = 1;
	printf("输入1-7的数字,获取我的工作日历:") ;
	scanf("%d", &day);
	
	switch (day){
		case 1:
			printf("睡觉");
			break;
		case 2:
			printf("看番");
			break;
		case 3:
			printf("吃好吃的");
			break;
		case 4:
			printf("打游戏");
			break;
		case 5:
			printf("散心");
			break;
		case 6:
			printf("去西校区找朋友玩");
			break;
		case 7:
			printf("出去玩");
			break;
		default:
			printf("输入有误,没有这个日历");
			break;	
	}
}

image.png

image.png

image.png

作业:

image.png

#include <stdio.h>

int main() {
    int month, year;
    printf("请输入月份:");
    scanf("%d", &month);

    switch (month) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            printf("该月有31天\n");
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            printf("该月有30天\n");
            break;
        case 2:
            printf("请输入年份:");
            scanf("%d", &year);
            // 判断闰年:能被4整除但不能被100整除,或能被400整除
            if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
                printf("该月有29天(闰年)\n");
            } else {
                printf("该月有28天(平年)\n");
            }
            break;
        default:
            printf("输入的月份无效,请输入1-12之间的数字\n");
    }

    return 0;
}

image.png