编译一个简单C程序时,我输入1000时,程序不是报输入错误,而是-900

img

#include <stdio.h>
int main()
{
int change,price;

printf("请输入商品价格:");
scanf("%d",&price);

if(0 <= price <=100)
{
    change = 100 - price;
    printf("应找您%d元",change);
    
}
else
{
    printf("输入错误");
     
}
return 0;

}

第9行改为:

if(price>=0 && price<=100)

change=1000-price吧?

你这if语句有问题吧,应该是if(price>=0&&prince<=100);

if(0 <= price <=100)
先是0 <= price 为 true,
true <= 100 为 true
所以就有了后面的100-1000 等于-900

if(0 <= price && price<=100) 应该这样写

关注我

if后面的语句错误,不满足C语言中的“与语句”