[英]Errors in switch and case statements
我在此函数中使用switch语句似乎有问题,但是一切对我来说都是正确的。
double pricePerUnit(int x, int y)
double price=pricePerUnit;
{
switch(pricePerUnit)
{
case '1':
if(estimatedQuality(x,y)<2)
{
price=0.5;
break;
}
这只是switch语句的一部分,还有更多情况。 但是,错误仅针对代码中的这些行。
error: parameter âpriceâ is initialized
error: old-style parameter declarations in prototyped function definition
error: switch quantity not an integer
error: âpriceâ undeclared (first use in this function)
error: (Each undeclared identifier is reported only once
error: for each function it appears in.)
我对C很陌生,所以这对我来说有点困惑。 如果有人可以帮助,那就太好了。 谢谢
您应该测试什么estimatedQuality
返回,而不是在调用之前执行测试。
double pricePerUnit(int x, int y) {
int quality = estimatedQuality(x, y);
double price;
if (quality < 2) {
price = 0.5;
} else if (quality < 4) {
price = 0.75;
} else if (quality == 4) {
price = 1;
} else if (quality == 5) {
price = 2;
}
return price;
}
您可以使用如下switch
:
double pricePerUnit(int x, int y) {
double price;
switch(estimatedQuality(x, y)) {
case 0:
case 1:
price = 0.5;
break;
case 2:
case 3:
price = 0.75;
break;
case 4:
price = 1;
break;
case 5:
price = 2;
break;
}
return price;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.