当我按0时,为什么我的程序没有停止? C程序

时间:2015-06-27 18:03:16

标签: 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 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");
                } 

        }       
}

1 个答案:

答案 0 :(得分:2)

你的while循环错了。它说,虽然钱不是零,或者买入为零,但要求支出金额。

它应该说while(money != 0 && buy > 0)所以只要他们有钱并且花钱(买入高于零),它就会要求支出金额。