1.if else多重嵌套(判断输入的3个数字哪个数字大)
#include <stdio.h>
int main()
{
int a,b,c;
scanf("%d %d %d", &a, &b, &c);
int max = 0;
if ( a>b ) {
if ( a>c ) {
max = a;
} else {
max = c;
}
} else {
if ( b>c ) {
max = b;
} else {
max = c;
}
}
printf("The max is %d\n", max);
return 0;
}
2.分段函数
#include <stdio.h>
int main(){
/*分段函数,f(x)= 1 (x < 0)
= 0 (x = 0)
= 2 (x > 0)*/
int x;
int a;
printf("请输入x的值:\n");
scanf("%d",&x);
if(x<0){
a=1;
}else if(x==0){
a=0;
}else{
a=2;
}
printf("当x的值为 %d 时,f(x)的值是 %d",x,a);
return 0;
}
注意:在第二个else if (x==0)时,条件是判断,即要使用 ==,使用=的话是给x赋值
3.由输入百分制成绩输出成绩等级
#include <stdio.h>
int main(){
int type;
printf("请输入您的成绩:");
scanf("%d",&type );
int grade;
grade=type/10;
printf("您的成绩等级是:");
switch(grade){
case 10:
case 9:
printf("A");
break;
case 8:
printf("B");
break;
case 7 :
printf("C");
break;
case 6:
printf("D");
break;
default :
printf("抱歉,您未及格。");
}
return 0;
}