# include
void main()
{
int I, k, a, b, c, d, e, f,g;
printf("请输入利润I:\n");
scanf("%d", &I);
a = I * (1.0f/10); //此处强制为浮点类型,不然a一直是0,看你的意思了
b = (I - 100000) * 75/1000;
c = (I - 200000) * 5/100;
d = (I - 400000) * 3/100;
e = (I - 600000) * 15/1000;
f = (I - 1000000) * 1/100;
g = (I - 1) / 100000;
switch(g)
{
case 0:
printf("奖金总数k=%d\n", a);
break;
case 1:
printf("奖金总数k=%d\n", a + b);
break;
case 2:
case 3:
printf("奖金总数k=%d\n", a + b + c);
break;
case 4:
case 5:
printf("奖金总数k=%d\n", a + b + c + d);
break;
case 6:
case 7:
case 8:
case 9:
printf("奖金总数k=%d\n", a + b + c + d + e);
break;
default :
printf("奖金总数k=%d\n", a + b + c + d + e + f);
break;
}
望采纳~
你的逻辑错了。。
而且。。switch case 只能是具体的数字,你可以用if else
int I, k, a, b, c, d, e, f;
printf("请输入利润I:\n");
scanf("%d", &I);
a = 100000 * (1/10);
b = (200000 - 100000) * 75/1000;
c = (400000 - 200000) * 5/100;
d = (600000 - 400000) * 3/100;
e = (1000000 - 600000) * 15/1000;
if(I <= 100000)
printf("奖金总数k=%d\n", I/10);
else if(100000 < I && I <= 200000)
printf("奖金总数k=%d\n", a + (I - 100000) * 75/1000);
else if(200000 < I && I <= 400000)
printf("奖金总数k=%d\n", a + b + (I- 200000) * 5/100);
else if(400000 < I && I <= 600000)
printf("奖金总数k=%d\n", a + b + c + (I- 400000) * 3/100);
else if(600000 < I && I <= 1000000)
printf("奖金总数k=%d\n", a + b + c + d + (I- 600000) * 15/1000);
else
printf("奖金总数k=%d\n", a + b + c + d + e + (I -1000000) * 1/100);
我随便写的,应该没问题了
第一switch(表达式)后面要加花括号。也就是说整个语句体要被花括号括起来(复合语句)。
第二:case和常量表达式中间不用加空格吗?
第三:纠正一下,一楼的,defalut后面不用加break,执行defalut后自动退出switch
case。
switch(int(I/100000))
{
case 0:
printf("奖金总数k=%d\n", a);
break;
case 1:
printf("奖金总数k=%d\n", a + b);
break;
case 2:
case 3:
printf("奖金总数k=%d\n", a + b + c);
break;
case 4:
case 5:
printf("奖金总数k=%d\n", a + b + c + d);
break;
case 6:
case 7:
case 8:
case 9:
printf("奖金总数k=%d\n", a + b + c + d + e);
break;
default:
printf("奖金总数k=%d\n", a + b + c + d + e + f);
break;
}
case不能出现不等式,只能是等式