为什么循环没有停止?

时间:2019-04-24 02:12:30

标签: c loops

#include <stdio.h>
#include <stdlib.h>

#define NEWGEAR 15.0
#define USEDGEAR 5.0

int main() {
    int type;
    int num_items;
    float total;

    printf("Welcome to the market\n");
    printf("What would you like to do\n");
    printf("\t1-Buy New Gear\n");
    printf("\t2-Buy used gear\n");
    printf("\t3-Quit\n");

    scanf("%d", &type);

    while (type != 3) {
        switch (type) {
        case 1:
            printf("How many new items would you like?\n");
            scanf("%d", &num_items);
            total = num_items * 15.0;
            break;

这是代码不断询问您想要多少个新商品的地方?

        case 2:
            printf("How many old items would you like?\n");
            scanf("%d", &num_items);
            total = num_items * USEDGEAR;
            break;

代码一直在这里询问您要多少个旧物品?             情况3:                 休息;

        default:
            printf("Not a valid option\n");
            break;
        }
    }
    printf("Your total cost is %f\n",total);

    return 0;
}

我的选择都在循环

4 个答案:

答案 0 :(得分:2)

您永远不会将type变量更新为3,因此while循环永远不会终止。尽管您确实有一个break语句,但它会影响switch而不是包围它的while循环。

答案 1 :(得分:2)

您的循环逻辑有缺陷:

  • 您应该在循环内移动提示代码。
  • 您应该为每个答案更新total
  • 您应该测试scanf()是否成功转换了用户输入。

这是修改后的版本:

#include <stdio.h>

#define NEWGEAR  15.0
#define USEDGEAR  5.0

int main() {
    int type;
    int num_items;
    double total = 0;

    printf("Welcome to the market\n");
    for (;;) {
        printf("What would you like to do\n");
        printf("\t1-Buy New Gear\n");
        printf("\t2-Buy used gear\n");
        printf("\t3-Quit\n");

        if (scanf("%d", &type) != 1 || type == 3)
            break;

        switch (type) {
        case 1:
            printf("How many new items would you like?\n");
            if (scanf("%d", &num_items) == 1)
                total += num_items * 15.0;
            break;

        case 2:
            printf("How many old items would you like?\n");
            if (scanf("%d", &num_items) == 1)
                total += num_items * USEDGEAR;
            break;

        default:
            printf("Not a valid option\n");
            break;
        }
    }
    printf("Your total cost is %f\n", total);

    return 0;
}

答案 2 :(得分:2)

我认为这可以满足您想要实现的目标。

#include <stdio.h>
#include <stdlib.h>

#define NEWGEAR 15.0
#define USEDGEAR 5.0

int main() {
    int type = 0;
    int num_items;
    float total;

    printf("Welcome to the market\n");
    printf("What would you like to do\n");
    printf("\t1-Buy New Gear\n");
    printf("\t2-Buy used gear\n");
    printf("\t3-Quit\n");

    while (type != 3) {
      printf("Enter an option: ");
      scanf("%d", &type);

      if(type == 3)
        break;

      switch (type) {
        case 1:
            printf("How many new items would you like?\n");
            scanf("%d", &num_items);
            total = num_items * 15.0;
            break;
        case 2:
            printf("How many old items would you like?\n");
            scanf("%d", &num_items);
            total = num_items * USEDGEAR;
            break;
        default:
            printf("Not a valid option\n");
            break;
        }
    }
    printf("Your total cost is %f\n",total);

    return 0;
}

基本上,在用户完成案例1-2或默认情况后,如果用户要退出或执行另一种案例1或2的操作,程序将再次提示您选择。

答案 3 :(得分:0)

您可以尝试if(type != 3) 而不是使用while(type != 3)