#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
float a,sum;
printf("输入n,a的值:");
scanf("%d%f",&n,&a);
switch(n)
{
case 1:
sum=a+a*0.0225;
printf("%f\n",sum);
break;
case 2:
sum=a+a*2*0.0243;
printf("%f\n",sum);
break;
case 3:
sum=a+a*3*0.027;
printf("%f\n",sum);
break;
case 5:
sum=a+a*5*0.0288;
printf("%f\n",sum);
break;
case 8:
sum=a+a*8*0.03;
printf("%f\n",sum);
break;
default:
printf("错误\n");
}
}
总结:
1用switch语句来计算不同年份的本金与利息之和;
2注意switch与break的连用;
易错点:
注意case之后的值是整型还是字符型,与定义前一致;