新手求解答关于c语言代码问题

青鸾之旅 发布于 2013/03/03 17:33
阅读 880
收藏 0

【开源中国 APP 全新上线】“动弹” 回归、集成大模型对话、畅读技术报告”

我的代码哪错了?显示的是:ex1.c:7:10: error: switch quantity not an integer
ex1.c:9:2: error: case label does not reduce to an integer constant
ex1.c:12:2: error: case label does not reduce to an integer constant
ex1.c:15:2: error: case label does not reduce to an integer constant


#include <stdio.h>
main()
{
float x,y;
  prntf("plese input a count for x:");
scanf("%f",&x);
switch (x)
{
case x<1:
y = x;
break;
case x>=1||x<10:
y = 2*x - 1;
break;
case x>=10:
y = 3*x - 11;
}
printf("the result 'y' is :%f\n",y);


return 0;
}

加载中
0
dreamers
dreamers
如果 你想根据 case x>y的意思 来做的话,可以选择if (){}。case后不能跟一个布尔值。

0
CrazyaStone
CrazyaStone

switch quantity not an integer (case 后面就整型和字符型变量,不要用 float型)

The expression of each case label shall be an integer constant expression and no two of
the case constant expressions in the same switch statement shall have the same value
after conversion.

c99标准。

0
lwdxinghuo
lwdxinghuo
#include <stdio.h>

main()
{
	float x,y;
	int a[] = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2};
 
  	printf("plese input a count for x:");
	scanf("%f",&x);

	int c = a[x < 10 ? (int)x:10];;

	switch (c)
	{
		case 0:
			y = x;
			break;
		case 1:
			y = 2*x - 1;
			break;
		case 2:
			y = 3*x - 11;
	}

	printf("the result 'y' is :%f\n", y);

	return 0;
}
0
lwdxinghuo
lwdxinghuo
#include <stdio.h>

void fun0(float x)
{
	printf("the result 'y' is :%f \n", x);
}

void fun1(float x)
{
	printf("the result 'y' is :%f \n", 2*x - 1);
}

void fun2(float x)
{
	printf("the result 'y' is :%f \n", 3*x - 11);
}

typedef void FUN(float x); 

main()
{
	float x,y;
	int a[11] = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2};
 
 	FUN *fun[3] = { fun0, fun1, fun2 };
 
  	printf("plese input a count for x:");
	scanf("%f",&x);

	int c = a[x < 10 ? (int)x:10];
	
	(*fun[c])(x);

	return 0;
}
0
青鸾之旅
青鸾之旅
我自己试出来,但是谢谢你们了,纠结不能用bool。。。。www = =
0
黃健民
黃健民
switch表达式怎么可以是浮点数呢!又是个不好好看错误信息娃~
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部