为什么';当我按0时,我的程序不会停止吗?C程序

为什么';当我按0时,我的程序不会停止吗?C程序,c,C,我做了一个简单的程序,问你是否可以进入一个酒吧,如果你的年龄。然后问你愿意花多少钱,然后问你第一次买多少钱。它继续询问您是否想购买更多,但是如果我不想购买更多,我可以输入值“0”,但当我输入“0”时,它不会关闭程序 代码如下: #include <stdio.h> int main() { int age, legal = 18; float money, buy; printf("At the bar the security must ID the customers

我做了一个简单的程序,问你是否可以进入一个酒吧,如果你的年龄。然后问你愿意花多少钱,然后问你第一次买多少钱。它继续询问您是否想购买更多,但是如果我不想购买更多,我可以输入值“0”,但当我输入“0”时,它不会关闭程序

代码如下:

#include <stdio.h>

int main() {
int age, legal = 18;
float money, buy;

    printf("At the bar the security must ID the customers that look underage, what is your age?: ");
    scanf("%d", &age);

        if(age < legal){
            printf("\nYou are not allowed!\n");
        }

        else if(age >= legal){
            printf("\nYou may enter, and buy whatever you wish!\n");

            printf("\nHow much money are you willing to spend tonight?: ");
            scanf("%f", &money);

            printf("\nWith the %.2f dollars you want to spend tonight what is the price of food or beverage you wish to buy first?: ", money);
            scanf("%f", &buy);

            money = money - buy;

            while(money > 0 || buy==0) {
                printf("\nYou now have %.2f dollars remaining, what else would you like to buy? (Press 0 to stop if you don't wish to buy any more!!): ", money);
                scanf("%f", &buy);
                money = money - buy;
            }
                if(money == 0){
                    printf("\nYou have spent all the money that you wished to spend at the bar, we hope you enjoyed your stay!!\n");
                }

                else if(money > 0){
                    printf("\nYou still have %.2f dollars left, We hope you enjoyed your stay at the bar!!\n");
                } 

        }       
}
#包括
int main(){
年龄,法定=18岁;
流动资金,购买;
printf(“在酒吧,保安必须识别看起来未成年的顾客,你的年龄是多少?:”;
scanf(“%d”和年龄);
如果(年龄<法定年龄){
printf(“\n不允许您!\n”);
}
否则,如果(年龄>=法定年龄){
printf(“\n您可以输入并购买您想要的任何东西!\n”);
printf(“\n您今晚愿意花多少钱?:”;
scanf(“%f”和money);
printf(“\n连同你今晚想花的%.2f美元,你想先买的食物或饮料的价格是多少?:”,money);
scanf(“%f”、&buy);
金钱=金钱购买;
而(货币>0 | |购买==0){
printf(“\n您现在还有%.2f美元,您还想买什么?(如果您不想再买,请按0停止!!):”,money);
scanf(“%f”、&buy);
金钱=金钱购买;
}
如果(货币==0){
printf(“\n您已经花光了所有希望在酒吧消费的钱,希望您在这里过得愉快!!\n”);
}
如果(钱>0)则为else{
printf(“\n您还有%.2f美元,希望您在酒吧过得愉快!!\n”);
} 
}       
}

您的while循环错误。它说,当钱不是零,或者当购买是零时,询问消费金额


它应该说
while(money!=0&&buy>0)
这样,只要他们有钱并且正在消费(buy高于零),它就会询问消费金额。

请避免使用
float
double
来表示金钱,因为它们给出了不精确的比较。使用
int
以美分计算,并将输出格式设置为美元.cents。拆分
上的任何输入字符串以将其转换为美分。