8.C语言switch_case相关知识点及相关应用

1—选择判断的应用

#include <stdio.h>

int main(){
	int healthy;
	int rich;
	int handsome;
	int humorous;
	
	puts("healthy or not");
	scanf("%d",&healthy);
	
	if(healthy == 1){
		puts("handsome_rich or not");
		scanf("%d%d",&rich,&handsome);
		
		if(handsome && rich == 1){
			puts("humorous or not");
			scanf("%d",&humorous);
			
			if(humorous == 1){
				printf("宝宝\n");
			}else{
				printf("我们不合适\n");	
			}
		}else{
			printf("我们不合适\n");
		}
	}else{
		printf("我们不合适\n");
	}
	return 0;
}

2—switch_case

  • 意义
    在这里插入图片描述
  • 代码
    #include <stdio.h>
    
    int main(){
    	int a;
    	char b;
    	
    	printf("请输入一个数:\n");
    	scanf("%d",&a);
    	
    	switch(a){
    		case 1:
    			printf("第1个情况\n");
    		break;
    		
    		case 2:
    			printf("第2个情况\n");
    		break;
    		
    		case 3:
    			printf("第3个情况\n");
    		break;
    		default:
    			printf("第4个情况\n");
    	}
    	
    	
    	
    	
    	printf("请输入一个字符:\n");
    	getchar();
    	
    	scanf("%c",&b);
    	
    	switch(b){
    		case 'a':
    			printf("第a个情况\n");
    		break;
    		
    		case 'b':
    			printf("第b个情况\n");
    		break;
    		
    		case 'c':
    			printf("第c个情况\n");
    		break;
    		default:
    			printf("第d个情况\n");
    	}
    	return 0;
    }
    

3—switch练习学生成绩

#include <stdio.h>

int main(){
	int score;
	scanf("%d",&score);
	
	switch(score/10){
		case 0:
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
			printf("D\n");
		break;
		case 6:
		case 7:
			printf("C\n");
		break;
		case 8:
		case 9:
			printf("B\n");
		break;
		case 10:
			printf("A\n");
		break;
		default:
			printf("error\n");
	}
	puts("done\n");
	return 0;
}

4—路程折扣

#include <stdio.h>

int main(){
	int zhekou;//折扣
	int lucheng;//路程
	int danjia;//单价

	puts("请输入路程:");
	scanf("%d",&lucheng);

	if(lucheng < 250){
		zhekou = 0;
	}else if(lucheng >= 250 && lucheng < 500){
		zhekou = 2;
	}else if(lucheng >= 500 && lucheng < 1000){
		zhekou = 5;
	}else if(lucheng >= 2000 && lucheng < 3000){
		zhekou = 8;
	}else{
		zhekou = 15;
	}
	printf("折扣是:%.2f\n",(float)zhekou/100);
}

5—作业

  • 分段函数

    #include <stdio.h>
    
    int main(){
    	//1.定义一个变量x
    	int x;
    	//2.输入一个数字
    	scanf("%d",&x);
    	//3.x<1的情况
    	if(x < 1){
    		printf("%d",x);
    	//4.大于1小于10的情况
    	}else if(x >= 1 && x <= 10){
    		printf("%d",2*x-1);
    	//5.x大于等于10的情况
    	}else{
    		printf("%d",3*x-11);
    	}
        return 0;
    }
    
  • 学生成绩

    #include <stdio.h>
    
    int main(){
    	//1.定义一个变量x
    	int x;
    	//2.输入一个成绩
    	scanf("%d",&x);
    	//3.90分以上的情况
    	if(x >= 90){
    		printf("A");
    	//4.80到85的情况
    	}else if(x >= 80 && x <= 85){
    		printf("B");
    	//5.70到79的情况
    	}else if(x >= 70 && x <= 79){
    		printf("C");
    	//6.60到69的情况
    	}else if(x >= 60 && x <= 69){
    		printf("D");
    	//7.60以下的情况
    	}else if(x < 60){
    		printf("E");
    	}
    	return 0;
    }
    
  • 24
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值